A Tour of Logcat

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

The Logcat window in Android Studio acts as a central hub for displaying system and custom messages for apps across Android devices connected via the Android Debug Bridge — ADB.

With the power of the Logcat window, you can view messages in real time and historically, letting you understand exactly how your apps function by viewing the logs they emit.

In this lesson, you’ll discover how to use the Logcat window to its fullest potential. You’ll learn how to:

  • Navigate the Logcat window.
  • Send custom log messages from your app.
  • Customize the Logcat window, making it easier to read.
  • Filter your logs.
  • Use logs to help you find and fix bugs.

Project Setup

To illustrate a greater variety of messages in Logcat, the sample project for this lesson is a fully functional P2P chat app.

Registering Your App With Ditto

You’ll need to create a developer account with Ditto and build a personal app on its website. This gives you access to the authentication keys needed for the SDK. In a web browser, navigate to Ditto.live and click Get started. Then, follow the steps to create an account. The simplest way is to sign up with your existing Google account. Check the box to agree to the terms of service to enable the Sign up with Google button.

Setting Up Ditto SDK in Your App

The code that utilizes the Ditto SDK is already in place in the sample project. All you need to do is add your license keys.

Keeping Your Secrets Safe

You shouldn’t commit secrets, such as keystore passwords, API licenses, etc., to a version control system such as GitHub. Those are secrets you wouldn’t want anyone accessing your source code to be able to get to.

DITTO_APP_ID = "replace with your app ID"
DITTO_TOKEN = "replace with your token"

Breaking Down the Logcat Window

To become familiar with the Logcat window, you’ll have to produce some logs first.

Filtering

Next, ensure the filter “package:mine” is applied and the log level is set to Warn. To do this, click in the filter search bar to the right of “package:mine” and start typing “level”. As you do this, you’ll see various severity levels come up:

Log Messages Explained

Take a detailed look at one log:

Tags

In Logcat, a tag is a short, custom label you include in your log messages. It acts like a category identifier for your logs. Here’s what tags do:

Customizing Log Message Format

You can customize which fields appear in the log in Logcat. Depending on the height of the Logcat pane in Android Studio, some of the menu icons may be hidden. Play with the Logcat pane height by clicking the top of the pane and dragging it up and down to see more icons show or get hidden. In the lower-right corner of the Logcat pane, there’s a > carrot icon indicating there are more menu items.

Gemini

You can also ask Android Studio to explain in detail the meaning of any message in Logcat.

Adding Custom Logs

You can create and post any kind of log message to Logcat. Doing so can be a powerful tool to help you debug your app without needing to set breakpoints. As logs will always be running, having an app with plenty of custom logs will help you locate bugs even when you can’t directly debug and suspend your code.

import android.util.Log
private const val TAG = "MainActivity"

Info Logs

When you’re viewing logs, the main piece of information you need to know is where the app code is during an event. To do this, you can decorate your code with Info logs that simply state what has been called.

override fun onResume() {
  super.onResume()
  Log.i(TAG, "onResume() called.")
}
val missing = DittoSyncPermissions(this).missingPermissions()
Log.i(TAG, "Checking permissions for: '$missing'")

Log.i(TAG, "Checking permissions for: '${missing.contentToString()}'")

Summary

You’ve learned all about a powerful tool within Android Studio, Logcat. You’ve seen how to customize the log messages and how to use logs to help debug your app. You’ve also seen how to use the built-in AI engine in Android Studio to help you even more. Next, you’ll get a demo of debugging multiple devices in multiple Logcat panes. Get your whiskers ready! 😺

See forum comments
Download course materials from Github
Previous: Debugging with Android Studio's Logcat 🐛😼 Next: Demo