In this document
- logcat Message Format
- Displaying a Running App in the logcat Monitor
- Setting the Log Level
- Searching logcat Messages
- Filtering logcat Messages
- Configuring the logcat Header Display
- Moving Up and Down the Stack Trace
- Moving to the End of the Log
- Printing and Writing to a File
- Clearing and Restarting the Log
- Adding Log Messages to Your Code
Android Monitor includes a logcat Monitor that displays debug messages. The logcat Monitor displays system messages, such as when a garbage collection occurs, as well as messages that you can add to your app using the Log class. It displays messages in real time and also 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, the logcat Monitor shows the log output related to the most recently run app only.
When an app throws an exception, the logcat Monitor shows a message followed by the associated stack trace containing links to the code. This feature can help you fix errors and improve app operation.
As of Android Studio 2.2, the Run window also displays log messages for the current running app. Note that you can configure the logcat Monitor display, but not the Run window.
logcat Message Format
Every Android log message has a tag and a priority associated with it. The tag of a system
is a short string indicating the system component from which the message originates (for example,
ActivityManager). A user-defined tag can be any string that you find helpful, such
as the name of the current class (the recommended tag). You define it in a
method call, for example:
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
For example, the following log message has a priority of
V and a tag of
12-10 13:02:50.071 1901-4229/com.google.android.gms V/AuthZen: Handling delegate intent.
PID stands for process identifier and TID is thread identifier; they can be the same if there’s only one thread.
Displaying a Running App in the logcat Monitor
To display the log messages for a particular app:
- Meet the prerequisites and dependencies.
- Open an app project.
- Run the app on a hardware device or emulator.
- Display Android Monitor.
- Click the logcat tab.
- Optionally select a different device, emulator, or process.
By default, the logcat Monitor displays just the log messages for your app running on the device or emulator:
To change this default, see Filtering logcat Messages.
Setting the Log Level
You can control how many messages appear in the logcat Monitor by setting the log level. You can display all messages, or just the messages indicating the most severe conditions.
Remember that the logcat Monitor continues to collect all messages regardless of the log level setting. The setting just determines what the logcat Monitor displays.
To set the log level:
- 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.
Searching logcat Messages
To search the messages currently displayed in the logcat Monitor:
- Optionally select Regex if you want to use a regular expression search pattern.
- Type a character sequence in the search field .
- Press Enter to store the search string in the menu during this session.
- To repeat a search, choose it from the search menu. Select or deselect Regex as needed (the setting isn’t remembered).
The logcat Monitor display changes accordingly.
Filtering logcat Messages
One way to reduce the log output to a manageable level is to restrict it by using a filter.
Note: The filter applies to your full logcat history, not just those messages currently displayed in the logcat Monitor. Make sure your other display options are set appropriately so you can see the filter output you want to examine.
To define and apply a filter:
- In the filter menu, select a filter option:
- Show only selected application - Display the messages produced by the app code only (the default). The logcat Monitor filters the log messages using the PID of the active app.
- No Filters - Apply no filters. The logcat Monitor 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.
- If you selected Edit Filter Configuration, create or modify a
- 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. For more information, see logcat Message Format.
- Log Message - Optionally specify log message text. For more information, see logcat Message Format.
- Package Name - Optionally specify a package name. For more information, see logcat Message Format.
- PID - Optionally specify a process ID. For more information, see logcat Message Format.
- Log Level - Optionally select a log level. For more information, see Setting the Log Level.
- Regex - Select this option to use regular expression syntax for that parameter.
- Click + to add the filter definition to the left pane.
- When you’re finished, click OK. If you click Cancel, any filter additions or modifications are lost.
To remove a filter, select it in the left pane and click -.
If you don't think you see the log messages you want, try selecting No filters and searching for particular log messages.
Configuring the logcat Header Display
To customize the header display to show just the information you’re interested in:
- Select Use Soft Wraps to see the entire message and prevent it from running off of the right edge.
- Click Configure Logcat Header to specify elements of the messages that you want to show or hide, and then click OK.
For more information about message elements, see logcat Message Format.
Moving Up and Down the Stack Trace
When the app throws an exception, the message includes a stack trace of method calls. logcat Monitor lets you quickly locate stack traces in the log and view the associated code in the Code Editor. If needed (and possible), the decompiler derives source code that you can view.
To move up and down the stack trace, and view the associated code in the Code Editor:
- Click Up the Stack Trace to move to the previous method in relation to the current position in the log.
- Click Down the Stack Trace to move to the next method in relation to the current position in the log.
Moving to the End of the Log
Clicking a particular message stops the display of messages.
To quickly move to the end of the log to see the real-time message flow:
- Click Scroll to the End .
- Press the End key.
- Scroll or press the Page Down key until you reach the end.
Printing and Writing to a File
To preserve log information, you can send the log to a printer, write the log to a PDF file, or copy and paste the log into a text file.
To print the log or write it to a PDF file:
- Click Print .
- In the Android Studio Print dialog, optionally change print parameters, and then click Print.
- In the operating system Print dialog, optionally change print parameters, and then click Print.
You can set the parameters to send the log to a printer or write it to a PDF file.
To copy the log to a text file:
- In the logcat Monitor, select and then copy log text.
- Open a text editor and paste the text into a file.
Press Control+A (Command+A) to select all.
Clearing and Restarting the Log
To clear (flush) the entire log:
- Click Clear logcat .
If there's a problem and the log is no longer progressing, you can restart the log:
- Click Restart .
Adding Log Messages to Your Code
allows you to create log messages that appear
in the logcat Monitor. Generally, you should use the following log methods,
listed in order from the highest to lowest priority
(or, least to most verbose):
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.
For each log method, the first parameter should be a unique tag and the
second parameter is the message. The
tag of a system log message is a short string indicating the system component
from which the message originates (for example,
). Your tag can be any string that you find helpful, such as the name of
the current class.
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);
Note: Tag names greater than 23 characters are truncated in the logcat output.