Proper Logging from Android Application

In this short article, we will dispel a simple confusion about logging in Android.

The android.util.Log class has various methods to perform logging at different levels. For example, v() is for verbose and i() is for information.

static String TAG = "MyApp";

Log.v(TAG, "Hello World");

The default log level is INFO. From the adb shell, you can change the log level to DEBUG, VERBOSE etc. using this command:

setprop  log.tag.MyApp  DEBUG

This looks straightforward. But, here is the catch. No matter what the log level is, every log entry always gets logged. If this is confusing to you, an example will help. In the examples above, we have set the log level to DEBUG. From the code we do a VERBOSE level log entry. You will think that logcat will not show this entry. That is not true. As I said, 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?

The answer is simple. You need to change your code and avoid logging based on the current log level setting. So, our example code will look like this.

if (Log.isLoggable(TAG, Log.VERBOSE)) {
    Log.v(TAG, "Hello World");

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.

You can simplify your life by creating a utility class that hides some of the details.

public class Logger {
    static String TAG = "MyApp";
    public static void v(String str) {
        if (Log.isLoggable(TAG, Log.VERBOSE)) {
            Log.v(TAG, str);
    public static void v(String str, Throwable t) {
        if (Log.isLoggable(TAG, Log.VERBOSE)) {
            Log.v(TAG, str, t);

Now, logging becomes super easy from your application:

Logger.v("Hello World");

To view verbose level log during development, run this command from adb shell.

setprop  log.tag.MyApp  VERBOSE

End users will not see the log entries since the default log level is INFO.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s