Android Studio Logcat Explained



Introduction:

Hey guys, so many of you seem to not use Logcat properly, so here is a post on how you can use Logcat and how you can solve your errors with it. Hope it helps. 

What is Logcat?

The Logcat window in Android Studio displays system messages, such as when a garbage collection occurs, and messages that you added to your app. When an app throws an exception, Logcat shows a message followed by the associated stack trace containing links to the line of code.

View your app logs:

To display the log messages for an app:

  1. Build and run your app on a device.
  2. Click View > Tool Windows > Logcat (or click Logcat in the tool window bar).

By default, Logcat displays just the log messages for your app running on the device

The Logcat toolbar provides the following buttons:

  1. 1. Clear logcat : Click to clear the visible log.
  2. 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. 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 corresponding 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. 4. Use soft wraps : Click to enable line wrapping and prevent horizontal scrolling (though any unbreakable strings will still require horizontal scrolling).
  5. 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. 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. 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. 8. Screen capture : Click to capture a screenshot.
  9. 9. Screen record : Click to record a video of the device (for a maximum of 3 minutes).

Different Log Levels:
  • 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.

How can you find your error with Logcat?
    
When your app crashes, Logcat will display an error message like this:



You can see, the app crashed because we accessed an array at index 1, but it has one element. To see the line of code this error occurred, click on the blue link.
Of course, this is a simple example. If you don't know what an error message means, you just copy it, paste it in Google and Stackoverflow will tell you what's wrong.

LOGCAT IS A POWERFUL TOOL, USE IT.

Sharique Ahmed Ansari
Android/Software Developer 
@Reliance Jio Platform Ltd.

Comments

Popular posts from this blog

How to implement Google FCM Push Notification in Android Application