Logging

suggest change

Any quality Android application will keep track of what it's doing through application logs.

These logs make it easier for the developer to diagnose what is going on with the application. Full Android documentation can be found here, but a summary follows:

Basic Logging

The Log class is the main source of writing developer logs, by specifying a tag and a message. The tag is what you filter on to identify which lines come from your particular Activity. Simply call

Log.v(String tag, String msg);

And the Android system will write a message to the logcat:

07-28 12:00:00.759 24812-24839/my.packagename V/MyAnimator: Some log messages
 └ time stamp             |  app.package┘     |    └ any tag  |
     process & thread ids ┘          log level┘               └ the log message

TIP: Notice the process id and the thread id. If they are the same - the log is coming from the main/UI thread!

Any tag can be used, but it is common to use the class name as a tag:

public static final String tag = MyAnimator.class.getSimpleName();

—

Log Levels

The Android logger has 6 different levels, each of which serves a certain purpose:

E/MyApplication: Process: com.example.myapplication, PID: 25788 com.example.SomeRandomException: Expected string, got 'null' instead

A quick glance at the logs and it is obvious that the file was empty.

—

Things to Consider When Logging:

Although logging is a powerful tool that allows Android developers to gain a greater insight into the inner workings of their application, logging does have some drawbacks.

Log Readability:

It is common for Android applications to have several logs running at the same time. As such, it is very important that each log is easily readable and only contains relevant, necessary information.

Performance:

Logging does require a small amount of system resources. In general, this does not warrant concern, however, if overused, logging may have a negative impact on application performance.

Security:

Recently, several Android applications have been added to the Google Play marketplace that allow the user to view logs of all running applications. This unintended display of data may allow users to view confidential information. As a rule of thumb, always remove logs that contain non-public data before publishing your application to the marketplace.

—

Conclusion:

Logging is an essential part of an Android application, because of the power it gives to developers. The ability to create a useful log trace is one of the most challenging aspects of software development, but Android's Log class helps to make it much easier.

For more documentation and examples visit Logging and using Logcat

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents