Home Tutorials Training Consulting Books Company Contact us


Get more...

This tutorial describes how to create and view log statements in Android applications.

1. Logging in Android

1.1. The log system of Android

The Android system uses a centralized system for all logs. The application programmer can also write custom log messages. The tooling to develop Android applications allows you to define filters for the log statements you are interested in.

1.2. Create log statements

To write log statements, you use the android.util.Log class with the following methods:

  • Log.v()

  • Log.d()

  • Log.i()

  • Log.w()

  • Log.e()

  • Log.wtf()

They are sorted by relevance with Log.i() being the least important one. The first parameter of these methods is the category and the second is the message.

Typically you create a Constants interface in your Android application and provide your log category as a field.

// package declaration left out, use your application package

public interface Constants {
    String LOG = "com.vogella.testapp";
}

Android advises that a deployed application should not contain logging code. The Android development tools provide the BuildConfig.DEBUG flag for this purpose. 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.

The following example show how to write an error log message.

if (BuildConfig.DEBUG) {
    Log.e(Constants.TAG, "onCreate called");
}