Thursday, June 12, 2014

Logging with Android

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

1. Logging in 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.

2. Create log statements

To write log statements, you use the android.util.Log class with the Log.v()Log.d()Log.i()Log.w()Log.e() or Log.wtf()method. 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 flag as a field.
package com.vogella.android.first;

public interface Constants {
  String LOG = "com.vogella.android.first";
} 
Android advises that a deployed application should not contain logging code. The Android development tools provide theBuildConfig.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. This message is visible in the LogCat view in Eclipse.
if (BuildConfig.DEBUG) {
  Log.e(Constants.TAG, "onCreate called");
} 

3. Viewing log messages with the LogCat view

You can see the Android log statements via the LogCat view.
You can open this view via the Window → Show View → Other... → Android → LogCat menu entry.
Showing the LogCat view
The LogCat view allows you also to define a filter for the log messages, e.g., for your category. Press the + sign to create a new filter.

4. Exercise: Using log statements and the LogCat view

Open the LogCat view. It can be opened via the Window → Show View → Other... → Android → LogCat menu entry.

5. Exercise: Using log statements and the LogCat view

In your com.vogella.android.first project create the following interface to define a constant for your log statements.
package com.vogella.android.first;

public interface Constants {
  String LOG = "com.vogella.android.first";
} 
Add a log statement to your onCreate method.
@Override
protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);

  if (BuildConfig.DEBUG) {
    Log.d(Constants.LOG, "onCreated called");
  }

  setContentView(R.layout.activity_main);
} 
Run your application and check that you see your log statement in the LogCat view.

6. Exercise: Create filter in LogCat

Create a new filter in the view to only see logs based on your category. If you followed the exercises, your category is "com.vogella.android.first".
Define a filter for the Logcat view
Run your application application and validate that you see your log statement in the LogCat view based on your defined filter.
Define a filter for the Logcat view

7. Support free vogella tutorials

Maintaining high quality free online tutorials is a lot of work. Please support free tutorials by donating or by reporting typos and factual errors.

7.1. Thank you

Please consider a contribution if this article helped you.
Flattr this

7.2. Questions and Discussion

If you find errors in this tutorial, please notify me (see the top of the page). Please note that due to the high volume of feedback I receive, I cannot answer questions to your implementation. Ensure you have read the vogella FAQ as I don't respond to questions already answered there.

8. Links and Literature

8.1. Source Code

8.3. vogella Resources

vogella Training Android and Eclipse Training from the vogella team
Android Tutorial Introduction to Android Programming
GWT Tutorial Program in Java, compile to JavaScript and HTML
Eclipse RCP Tutorial Create native applications in Java
JUnit Tutorial Test your application
Git Tutorial Put all your files in a distributed version control system

No comments:

Post a Comment