Logging - The Way Of
BuildConfig.DEBUG
In recent versions of the Android Developer Tools (ADT) for Eclipse, theres a class called BuildConfig
which is automatically generated by the build.
This class is updated automatically by Androids build system (like the R class), and it contains a static
final boolean called DEBUG, which is normally set to true.
This flag will be automatically set to false if you export the Android application for deployment.
During development it will be set to true, therefore allowing you to see your logging statements during
development.
This new flag is intended to be used as a check for debug-only functions. For example, you can use it to
control logging, like below:
if (BuildConfig.DEBUG) {
Log.v(TAG, "onCreate()");
}
Log Levels
The android.util.Log class has various methods to perform logging at different levels.
static final
private String TAG = "MyCoolFragment";
Log.v(TAG, "onCreate()");
The default log level is INFO. From the adb shell, you can change the log level to
VERBOSE
DEBUG
INFO
WARN
ERROR
using this command:
./adb shell setprop log.tag.MyCoolFragment DEBUG
This looks straightforward. But, here is the catch: all log entries made from the code will always get
logged!
So, why bother setting the log level? More importantly, how do you disable logging from the release
version?
Luca Sepe
[email protected]
Logging - The Way Of
Disabling/Enabling logging
To avoid logging based on the current log level, you need to change your code:
if (Log.isLoggable(TAG, Log.VERBOSE)) {
Log.v(TAG, "onCreate()");
}
Now, this code will avoid a log entry if the level is set to something coarser like DEBUG or INFO.
This is why wrapping your log calls in isLoggable() is not just an optimization, it is absolutely
essential.
We can simplify ours life by creating a utility class that hides some of the details and we can still using
as usual:
static final
private String TAG = LOG.makeLogTag("cool_fragment");
Log.v(TAG, "onCreate()");
To view verbose level log during development, run this command from adb shell.
./adb shell setprop log.tag.MyAPP_cool_fragment VERBOSE
End users will not see the log entries since the default log level is INFO.
MyAPP prefix is a prefix that you can specify in the LOG class (see below).
LOG.makeLogTag("cool fragment"); is a conveninet method that create the log tag
prepending the prefix.
Log utility class
public class LOG {
static final
private String LOG_PREFIX = "MyAPP_";
static final
private int LOG_PREFIX_LENGTH = LOG_PREFIX.length();
static final
private int MAX_LOG_TAG_LENGTH = 23;
static
public String makeLogTag(String str) {
if (str.length() > MAX_LOG_TAG_LENGTH - LOG_PREFIX_LENGTH) {
return LOG_PREFIX +
str.substring(0, MAX_LOG_TAG_LENGTH - LOG_PREFIX_LENGTH - 1);
}
return LOG_PREFIX + str;
}
Luca Sepe
[email protected]
Logging - The Way Of
static
public void d(final String tag, String message) {
if (BuildConfig.DEBUG &&
android.util.Log.isLoggable(tag, android.util.Log.DEBUG)) {
android.util.Log.d(tag, message);
}
}
static
public void d(final String tag, String message, Throwable cause) {
if (BuildConfig.DEBUG &&
android.util.Log.isLoggable(tag, android.util.Log.DEBUG)) {
android.util.Log.d(tag, message, cause);
}
}
static
public void v(final String tag, String message) {
if (BuildConfig.DEBUG &&
android.util.Log.isLoggable(tag, android.util.Log.VERBOSE)) {
android.util.Log.v(tag, message);
}
}
static
public void v(final String tag, String message, Throwable cause) {
if (BuildConfig.DEBUG &&
android.util.Log.isLoggable(tag, android.util.Log.VERBOSE)) {
android.util.Log.v(tag, message, cause);
}
}
static
public void i(final String tag, String message) {
android.util.Log.i(tag, message);
}
static
public void i(final String tag, String message, Throwable cause) {
android.util.Log.i(tag, message, cause);
}
static
public void w(final String tag, String message) {
android.util.Log.w(tag, message);
}
static
public void w(final String tag, String message, Throwable cause) {
android.util.Log.w(tag, message, cause);
}
static
public void e(final String tag, String message) {
android.util.Log.e(tag, message);
}
Luca Sepe
[email protected]
Logging - The Way Of
static
public void e(final String tag, String message, Throwable cause) {
android.util.Log.e(tag, message, cause);
}
private LOG() {
Luca Sepe
[email protected]