Android Logging

This post describes how to create log statements in Android applications.

Android uses the android.util.Log class for logging. This class supports various levels (VERBOSE, DEBUG, INFO, WARN, ERROR) and contains numerous methods to print debugging information to the logcat utility.Logcat is visible in the debug perspective in Eclipse or by running adb logcat on the command line. You can read more about it here.
Log.w("MyTag", "Mymessage");
This code will output a warning with the tag MyTag and the message "Mymessage".
The first parameter of these method is the category and the second is the message.

Usually you use this code:
public static final String TAG = "";
It is very important to turn down logging when you deploy your app to the market.
Google advises that a deployed application should not contain logging code.

We can find a very good example in Google I/O 2012 app.
   public static void LOGD(final String tag, String message, Throwable cause) {
        if (Log.isLoggable(tag, Log.DEBUG)) {
            Log.d(tag, message, cause);

   public static void LOGV(final String tag, String message) {
        if (BuildConfig.DEBUG && Log.isLoggable(tag, Log.VERBOSE)) {
            Log.v(tag, message);

   public static void LOGI(final String tag, String message) {
        Log.i(tag, message);
The benefit here is you won’t have to worry about removing print lines or maintaining variables for levels that you might forget to change before building your release apk.

The ADT provides the BuildConfig.DEBUG flag.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 allow you to see your logging statements during development.

All log levels are written to logcat regardless of what the current log level is. The isLogabble() method can be used as an optimization for your apps to prevent sending unneeded log statements to logcat. You can also use the adb logcat command to filter a subset of the logging levels even if the logging is set to verbose.

If you read the information on Log.isLoggable(), you'll notice that the default logging level is INFO. Anything less than that (DEBUG and VERBOSE) will cause this method to return false.

You can tune your logging per TAG if you choose, either with a prop file or via adb. 
The nice thing about this is you can dynamically turn logging up/down/off for each individual TAG in your app without the need for manually commenting out Log lines, or implementing your own checks.

You can change the default level by setting a system property: 
Simply run adb shell setprop log.tag.<YOUR_LOG_TAG> <LEVEL>
where level is either VERBOSE, DEBUG, INFO, WARN, ERROR (Be care that the log.tag is necessary).

You can also create a local.prop file that with the following in it: 'log.tag.<YOUR_LOG_TAG>=<LEVEL>' and place that in /data/local.prop, but this will work only in a emulator or a rooted device.

Popular posts from this blog

Expand and collapse animation

How to centralize the support libraries dependencies in gradle

A Material styled AlertDialog