Logging with Android
This tutorial describes how to create and view log statements in Android applications.
Table of Contents
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.
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 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. This message is visible in the LogCat view in Eclipse.
if (BuildConfig.DEBUG) { Log.e(Constants.TAG, "onCreate called"); }
You can see the Android log statements via the LogCat view.
You can open this view via the → → → → menu entry.
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.
Open the LogCat view. It can be opened via the → → → → menu entry.
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.
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".
Run your application application and validate that you see your log statement in the LogCat view based on your defined filter.
Maintaining high quality free online tutorials is a lot of work. Please support free tutorials by donating or by reporting typos and factual errors.
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.
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