Logcat : Difference between Log levels | Android Studio Tutorials

Logcat : Difference between Log levels | Android Studio Tutorials

 Being an Android Developer, we all have used Log to check our API responses, Out Progress, etc.. But a major fraction of Android Developers still do not know the basic use of different Log levels or say, when to use particular Log level. So let's start with the intro.

The Logcat window in Android Studio displays system messages, such as when a garbage collection occurs, and messages that you added to your app with the Log class. It displays messages in real time and keeps a history so you can view older messages.

To display just the information of interest, you can create filters, modify how much information is displayed in messages, set priority levels, display messages produced by app code only, and search the log. By default, logcat shows the log output related to the most recently run app only.

When an app throws an exception, logcat shows a message followed by the associated stack trace containing links to the line of code.

The Logcat toolbar provides the following buttons:

  1. Clear logcat : Click to clear the visible log.
  2. Scroll to the end : Click to jump to the bottom of the log and see the latest log messages. If you then click a line in the log, the view pauses scrolling at that point.
  3. Up the stack trace  and Down the stack trace : Click to navigate up and down the stack traces in the log, selecting the subsequent filenames (and viewing the correspnding line numbers in the editor) that appear in the printed exceptions. This is the same behavior as when you click on a filename in the log.
  4. Use soft wraps : Click to enable line wrapping and prevent horizontal scrolling (though any unbreakable strings will still require horizontal scrolling).
  5. Print : Click to print the logcat messages. After selecting your print preferences in the dialog that appears, you can also choose to save to a PDF.
  6. Restart : Click to clear the log and restart logcat. Unlike the Clear logcat button, this recovers and displays previous log messages, so is most useful if Logcat becomes unresponsive and you don't want to lose your log messages.
  7. Logcat header : Click to open the Configure Logcat Header dialog, where you can customize the appearance of each logcat message, such as whether to show the date and time.
  8. Screen capture : Click to capture a screenshot.
  9. Screen record : Click to record a video of the device (for a maximum of 3 minutes).

The Log class allows you to create log messages that appear in logcat. Generally, you should use the following log methods, listed in order from the highest to lowest priority (or, least to most verbose):

  • Log.e(String, String) (error)
  • Log.w(String, String) (warning)
  • Log.i(String, String) (information)
  • Log.d(String, String) (debug)
  • Log.v(String, String) (verbose)

See the Log class description for a more complete list of options.

You should never compile verbose logs into your app, except during development. Debug logs are compiled in but stripped at runtime, while error, warning, and info logs are always kept.

A good convention is to declare a TAG constant in your class to use in the first parameter. For example, you might create an information log message as follows:

private static final String TAG = "MyActivity";
Log.i(TAG, "MyClass.getView() — get item number " + position);

The priority is one of the following values:

  • V: Verbose (lowest priority)
  • D: Debug
  • I: Info
  • W: Warning
  • E: Error
  • A: Assert

The basic difference between every Log level is : 

Verbose: Show all log messages (the default).

Debug: Show debug log messages that are useful during development only, as well as the message levels lower in this list.

Info: Show expected log messages for regular usage, as well as the message levels lower in this list.

Warn: Show possible issues that are not yet errors, as well as the message levels lower in this list.

Error: Show issues that have caused errors, as well as the message level lower in this list.

Assert: Show issues that the developer expects should never happen.

Post a Comment