简体   繁体   English

设置android studio logcat过滤器以仅显示异常和自定义日志

[英]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. 因此,我是Android Studio的新手,发现logcat功能对我很有帮助。 But the logcat shows me way too much info than that was needed. 但是logcat向我显示了太多的信息。 Now how do i set it's filter to only show crash reports, exceptions and custom logs(EX : Log.d.(TAG, "onCreate : Successs."); ) 现在我该如何设置它的过滤器,使其仅显示崩溃报告,异常和自定义日志(例如: 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. Logcat窗口中有一个部分,您可以在其中键入正则表达式以显示所需的内容。

In this example, it isn't showing anything, since the word "Successs" doesn't appear in my particular log. 在此示例中,它没有显示任何内容,因为“ Successs”一词未出现在我的特定日志中。
You can also use the menus and other items to set if ERROR or VERBOSE errors are displayed, or other filters. 您也可以使用菜单和其他项目来设置是否显示ERROR或VERBOSE错误,或其他过滤器。

在此处输入图片说明

@Johan Chersev here is a quick demo of working with the Logcat in Android Studio. @Johan Chersev是一个在Android Studio中使用Logcat的快速演示 In MainActivity an integer is being divided by zero. MainActivity中 ,整数除以零。 Java should raise an ArithmeticException ie java.lang.ArithmeticException: divide by zero ; Java应该引发ArithmeticException,java.lang.ArithmeticException:除以零 the goal is to log this exception to Logcat in the catch block. 目标是将此异常记录到catch块中的Logcat中。

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:- 要过滤掉Logcat中不需要的日志消息, 执行以下操作:

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. 打开Logcat工具窗口 ,可以通过在IDE底部单击名称为Logcat剥离工具按钮来完成。

Strip tool button with Logcat label . 剥去带有Logcat标签的工具按钮

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. 如果由于某种原因您看不到Logcat工具按钮条悬停在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 🎉🎉🎉). 现在有趣的部分(因为没有像android party party这样的聚会)。

Once logcat is open ensure your device is selected in the device dropdown menu (which shows a list of connected devices). 打开logcat后 ,请确保在设备下拉菜单 (显示已连接设备的列表)中选择了您的设备。

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) . 在这个演示中,我只希望记录称为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:- 如果您想进一步提高精确度并轻松查找日志消息,请使用TAG常量轻松过滤logcat消息,如下所示:

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. 单击logcat窗格右上角的下拉菜单,默认情况下已选中“仅显示所选应用程序” ;其称为过滤器下拉菜单。

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. 在“ 创建新的Logcat筛选器”窗口中,在“ 筛选器名称”字段中为您的筛选器命名,并最好在“ 日志标签”字段中输入相同的名称(为简便起见),然后单击“确定”。

On returning to Logcat only logs with the selected tags will pop-up. 返回Logcat后,只会弹出带有所选标签的日志。

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. PS我不能发布超过八个图像链接。 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. 在“ 创建新的Logcat筛选器”窗口中,在“ 筛选器名称”字段中为您的筛选器命名,并最好在“ 日志标签”字段中输入相同的名称(为简便起见),然后单击“确定”。

Create New Logcat Filter 创建新的Logcat筛选器

On returning to Logcat only logs with the selected tags will pop-up. 返回Logcat后,只会弹出带有所选标签的日志。

If this isn't the case click on the filter dropdown and select your log tag. 如果不是这种情况,请单击过滤器下拉菜单,然后选择您的日志标签。

Logcat filtered by custom tag Logcat按自定义标签过滤

Thats it you are done. 就是这样,您完成了。 Go forth and log like a boss. 像老板一样往前走。 😎 😎

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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