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");
}
1.3. Viewing log messages
See https://www.vogella.com/tutorials/AndroidStudioTooling/article.html#androidstudio_viewinglogentries to learn how to see log statements in your IDE.
2. Links and Literature
Nothing listed.
2.1. vogella Java example code
If you need more assistance we offer Online Training and Onsite training as well as consulting