Android Logging and using Logcat Logging

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Insert
> Step 2: And Like the video. BONUS: You can also share it!

Example

Any quality Android application will keep track of what it's doing through application logs. These logs allow easy debugging help for the developer to diagnose what's 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 can use to filter log messages by 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 serve a certain purpose:

  • ERROR: Log.e()
    • Used to indicate critical failure, this is the level printed at when throwing an Exception.
  • WARN: Log.w()
    • Used to indicate a warning, mainly for recoverable failures
  • INFO: Log.i()
    • Used to indicate higher-level information about the state of the application
  • DEBUG: Log.d()
    • Used to log information that would be useful to know when debugging the application, but would get in the way when running the application
  • VERBOSE: Log.v()
    • Used to log information that reflects the small details about the state of the application
  • ASSERT: Log.wtf()
    • Used to log information about a condition that should never happen.
    • wtf stands for "What a Terrible Failure".

Motivation For Logging

The motivation for logging is to easily find errors, warnings, and other information by glancing at the chain of events from the application. For instance, imagine an application that reads lines from a text file, but incorrectly assumes that the file will never be empty. The log trace (of an app that doesn't log) would look something like this:

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

Followed by a bunch of stack traces that would eventually lead to the offending line, where stepping through with a debugger would eventually lead to the problem

However, the log trace of an application with logging enabled could look something like this:

V/MyApplication: Looking for file myFile.txt on the SD card
D/MyApplication: Found file myFile.txt at path <path>
V/MyApplication: Opening file myFile.txt
D/MyApplication: Finished reading myFile.txt, found 0 lines
V/MyApplication: Closing file myFile.txt
...
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 Considering When Logging:

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

Log Readability:

It is common for Android Applications to have several logs running synchronously. 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 on 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



Got any Android Question?