[ANDROID STUDIO] Write and View Logs with Logcat

Gavin A. S. Cheng
6 min readJun 2, 2021

The “Logcat” window in Android Studio is used to display the log messages you want to collect.

View your app logs

The Logcat toolbar provides the following buttons:

Clear logcat: Click to clear the visible log.

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.

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.

Use soft wraps: Click to enable line wrapping and prevent horizontal scrolling (though any unbreakable strings will still require horizontal scrolling).

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.

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.

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.

Screen capture: Select the device and a process from the drop-down at the top of the window, and you can capture a screenshot.

Screen record: You can record an MP4 video from your device for a maximum of three minutes. You can, for example, use the video for your marketing materials or for debugging.

  • Bit Rate: Enter a bit rate. The default is 4 Mbps.
  • Resolution: Enter a width and height value in pixels. The value must be a multiple of 16. The default is the resolution of the device.
  • Show Taps: Enables visual feedback for taps.

Write log messages

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)

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

Logcat message format

Log.d(tag, message);

The priority is one of the following values:

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

The log message format is:

date time PID-TID/package priority/tag: message

PID stands for process identifier and TID is thread identifier; they can be the same if there’s only one thread.

Set the log level

You can control how many messages appear in logcat by setting the log level. You can display all messages, or just the messages indicating the most severe conditions.

In the Log level menu, select one of the following values:

  • 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.

Search logcat messages

To search the messages currently displayed in logcat:

  1. Optionally select Regex if you want to use a regular expression search pattern.
  2. Type a character sequence in the search field. The logcat output display changes accordingly.
  3. Press Enter to store the search string in the menu during this session.
  4. To repeat a search, choose it from the search menu. Select or deselect Regex as needed (the setting isn’t remembered).

Filter logcat messages

One way to reduce the log output to a manageable level is to restrict it by using a filter.

To define and apply a filter:

  1. In the filter menu, select a filter option:
  • Show only selected application: Display the messages produced by the app code only (the default). Logcat filters the log messages using the PID of the active app.
  • No Filters: Apply no filters. Logcat displays all log messages from the device, regardless of which process you selected.
  • Edit Filter Configuration: Create or modify a custom filter. For example, you could create a filter to view log messages from two apps at the same time.

After you define filters, you can also select them in the menu. To remove them from the menu, delete them.

2. If you selected Edit Filter Configuration, create or modify a filter:

2–1. Specify the filter parameters in the Create New Logcat Filter dialog:

  • Filter Name: Type the name of a filter you want to define, or select it in the left pane to modify an existing filter. The name can contain lowercase characters, underscores, and digits only.
  • Log Tag: Optionally specify a tag.
  • Log Message: Optionally specify log message text.
  • Package Name: Optionally specify a package name.
  • PID: Optionally specify a process ID.
  • Log Level: Optionally select a log level.
  • Regex: Select this option to use regular expression syntax for that parameter.

2–2. Click + to add the filter definition to the left pane. To remove a filter, select it in the left pane and click -.

2–3. When you’re finished, click OK.

If you don’t think you see the log messages you want, try selecting No filters and searching for particular log messages.

--

--