简体   繁体   中英

Set android studio logcat filters to show only exceptions and custom logs

So I am a newbie to Android Studio and found the logcat feature to be very much helpful to me. But the logcat shows me way too much info than that was needed. Now how do i set it's filter to only show crash reports, exceptions and custom logs(EX : Log.d.(TAG, "onCreate : Successs."); )

There is a section in the Logcat window, where you can type a Regular Expression to show what you want.

In this example, it isn't showing anything, since the word "Successs" doesn't appear in my particular log.
You can also use the menus and other items to set if ERROR or VERBOSE errors are displayed, or other filters.

在此处输入图片说明

@Johan Chersev here is a quick demo of working with the Logcat in Android Studio. In MainActivity an integer is being divided by zero. Java should raise an ArithmeticException ie java.lang.ArithmeticException: divide by zero ; the goal is to log this exception to Logcat in the catch block.

Code snippet.

package com.example.logcatdemo;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;

public class MainActivity extends AppCompatActivity {

    // To be passed as the tag in calls to methods of the android.util.Log class
    // e.g. Log.i(TAG, Message)
    private static final String TAG = "LogcatDemo";
    private int mNumber = 5;
    private int mDivisor = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        try {
            // Divide an integer by zero
            int result = mNumber / mDivisor;
        } catch (ArithmeticException aex) {
            // Send the exception details to logcat
            Log.e(TAG, "An error occurred in onCreate(Bundle savedInstanceState). See Details:-\n" + aex);
        }
    }
}

To filter out unwanted log messages in Logcat do the following:-

Open the Logcat tool window which can be done by clicking the strip tool button with the name Logcat on it at the bottom of the IDE.

Strip tool button with Logcat label .

If for some reason you don't see the Logcat tool button strip hover over the gray square button in the bottom left corner of the IDE.

Gray square button in bottom left corner

Or go to View then hover over Tool Windows.

View then Tool Windows

Now the fun part (Cause ain't no party like an android party 🎉🎉🎉).

Once logcat is open ensure your device is selected in the device dropdown menu (which shows a list of connected devices).

Device dropdown menu

Ensure your app's package name is selected in the dropdown menu that lists app packages.

App package dropdown list

You can specify the level of log messages displayed. In this demo I only want exceptions logged as I called log.e(tag, msg) . So I'll select error in the log level dropdown menu.

Log levels dropdown menu

If you want further precision and make finding your log messages easy peasy filter the logcat messages using a TAG constant as follows:-

Click the dropdown menu in the top right corner of the logcat pane that has the "Show only selected application" selected by default;its called the filter dropdown.

Filter dropdown

It currently only shows messages from your app.

In the resulting dropdown list select Edit Filter Configuration to create a new custom filter.

Edit Filter Configuration

In the Create New Logcat Filter window give your filter a name in the Filter Name field and preferably enter the same name (for simplicity or easy recall) in the Log Tag field then click ok.

On returning to Logcat only logs with the selected tags will pop-up.

If this isn't the case click on the filter dropdown and select your log tag .

Thats it you are done. Go forth and log like a boss. 😎

PS I couldn't post more than eight image links. So the last two images of the answer are as follows.

In the Create New Logcat Filter window give your filter a name in the Filter Name field and preferably enter the same name (for simplicity or easy recall) in the Log Tag field then click ok.

Create New Logcat Filter

On returning to Logcat only logs with the selected tags will pop-up.

If this isn't the case click on the filter dropdown and select your log tag.

Logcat filtered by custom tag

Thats it you are done. Go forth and log like a boss. 😎

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM