Working with Logcat

Logcat is an important part of everyday development. If you see one of those "Force Close" or "Has Stopped" dialogs, the first thing you will want to do is examine the Java stack trace that is associated with this crash. These are logged to a facility known as Logcat, its purpose is to display all logs coming from your device. It displays logs from an emulator or a real device connected.

We can see the following messages in Logcat:

  • System.out.println
  • exceptions
  • android.util.Log

To properly log messages from your application you should use the android.util.Log class. This class defines the familiar informational, warning, and error methods that you can filter in the Logcat pane to see just what you want to see. Every log message has a tag associated with it, which is used to identify the source of a log message. It usually identifies the class or activity where the log call occurs.

Here are some sample Log commands categorized by severity:

Log.v("TAG", "Verbose level message");
Log.d("TAG", "Debug level message");
Log.i("TAG", "Information level message");
Log.w("TAG", "Warning level message");
Log.e("TAG", "Error level message");"TAG", "Assert level message");

It's best to use the appropriate log level for the message you want to log. It is recommend to define a unique debug tag string for each class so that you can easily track down where a log message originated. You can use this tag to filter the logging data and find only the messages you are interested in.

The recommended way to define Tag string:

private static final String TAG = "MyActivity";
Log.v(TAG, "Verbose level message");

How to open Logcat?

  1. By keyboard shortcut alt + 6

  2. By clicking the main menu bar View > Tool Windows > Logcat.
    Logcat menubar

  3. By clicking the Logcat Tool Button from the bottom of Android Studio.
    Logcat tool button

The following Logcat pane will appear on bottom of Android Studio.

Logcat window

Following is the description of some important options highlighted in above image:

  1. Device selection menu

  2. App selection menu

  3. Filter messages based on log level, where messages for your chosen level or higher will be displayed

  4. Use the search field to filter items based on a search string

  5. When this menu is set to Show only selected application, only those messages relating to the app selected in the menu marked as 2 will be displayed in the Logcat panel. Choosing No Filter will display all the messages generated by the device or emulator.

Logcat levels

To get the most out of your application, Logcat has several levels of log messages, so Android Studio provides multiple ways of filtering the logcat output. One method is to use the Log Level dropdown menu, to filter based on log level, open this dropdown and select anything other than the default Verbose option.
Logcat levels menu

  1. Verbose: Display all log messages

  2. Debug: Displays log messages that are useful during development

  3. Info: Displays expected log messages for regular usage

  4. Warn: Displays possible issues that are not yet errors

  5. Error: Displays issues that have caused errors

  6. Assert: Displays issues that should never happen

Making a custom level in Logcat

Every Android log message includes a tag. You can use these tags with filters defined in Logcat. To open Logcat pane, click "Show only selected application" drop-down menu from the top right and select "Edit Filter Configuration". This opens a "Create New Logcat Filter" dialog:
New Logcat Filter Window

Provide the following information to create a new filter:

  • Filter name: give a unique name.

  • Log Tag: Every log message has a tag associated with it, which indicates the system component the message originated from. If you want to see messages that originate from a certain system component only, you can enter that component's tag here. It is used to identify the source of a log message. It usually identifies the class or activity where the log call occurs.

  • Log Message: If you only want to see messages that contain certain elements or character strings, specify them in the Log Message field.

  • Package Name: If you want your filter to display messages that relate to a certain package only, enter this package name here.

  • PID: If you only want to see messages that refer to a specific process, enter that process ID here.

Customizing log messages font colors and style

Open Android Studio Settings from the main menu File > Settings or by pressing keyboard shortcut Ctrl + Alt + S. In the search field, enter Logcat. Editor > Color & Fonts > Android Logcat preferences appears:

To change these options, you must save a copy of the current Theme by clicking the Save as button and enter some suitable name. Now select each log level one by one and apply your own font and background color, make log message bolt or italic and etc.

Note: before starting the customization, uncheck the Use inherited attributes box, otherwise you won't be able to edit these options.

  • Select a log level from the list (e.g. Assert)

  • If you want to change font color, check Foreground and click its color box to customize the font color

  • To change the background color of font, check Background and click its color box to customize the background color

  • Check Error stripe mark to display a mark at the end of message and click its color box to customize the mark color

  • Check Effects then select the effect from the drop down menu (e.g. underline, strikeout, or dotted line etc) and click its color box to customize the mark color.

To apply changes, click on Apply and then OK.

How to clear logs

The "trash can" icon atop the tool strip on the left is the "clear log" tool.

Clicking it will appear to clear Logcat. It definitely clears your LogCat view, so you will only see messages logged after you cleared it, though, that this does not actually clear the logs from the device or emulator.

Note: Log has impact on performance. Excessive logging impacts device and application performance. At a minimum, debug and verbose logging should be used only for development purposes and removed prior to application publication.