9. Logging in Android

9.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.

9.2. Create log statements

To write log statements, you use 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.

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.