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.


4 thoughts on “Proper Logging from Android Application

    • Log levels are set for tags. There is no hierarchical log level like you see in other logging packages like Log4J. There may be libraries that you can use for this. I am not sure.

  1. > isLoggable() is not just an optimization, it is absolutely essential.
    I tend to disagree: as isLoggable() works on a tag basis and tags tend to be the class name, on a big project it would require a huge effort to handle them and change them in debug/release. It’s likely to be easier if the tags were a well defined set, with a defined convention, but still prone to mistakes and (possibly) clashes with the v()/d()/i()/w()/e() convention.
    Unless it’s accurately organised it seems a more complex way to handle logs than using a gradle constant.

    • Interesting point. So far in all my projects I have used a single tag for all logs. So the approach described here works well. For a complex hierarchical logging I will use a third party library.

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 )

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.