This article describes several tips on how to filter the ADB logcat output from the shell command line.

Source: Internet
Author: User
Tags regular expression system log egrep


This article describes several tips on how to filter the ADB logcat output from the shell command line .



Development often see other people's log like a flood of instant fill the screen, to their useful information is drowned, affect the mood also affect efficiency. Here are a few of the filtering methods I know. 1. Show only the required output, whitelist



The most convenient of course is to use grep filtering through the pipeline, which can be matched using grep's powerful regular expression. Simply match a string in a row, such as MyApp:


ADB Logcat | grep myapp
adb logcat | grep-i myapp #Ignore case.
adb logcat | grep--color = auto-i myapp #Set the matching string color. For more settings, see grep Help.


The advanced point can be matched using grep's regular expression. For example, the previous example matches the MyApp anywhere in a row, and can be set to match tag only. The default log output is as follows, and the corresponding expression is modified if the output format has been modified.


I/cacheservice (  665): Preparing diskcache for all thumbnails.


You can see that the tag starts with the third character at the beginning of a line and writes the expression according to this:


ADB Logcat | grep "^. MyApp "





Depending on the format, you can also set a log that displays only a priority, and then match the first character in the beginning of a line. For example, only the output of the Error level tag MyApp is displayed:


ADB Logcat | grep "^e.myapp"


Of course, you can match more than one, using | Split multiple matching expressions to add escape characters. For example, to match the output of the tag for MyApp and myactivity:


ADB Logcat | grep "^. myapp\|^. MyActivity "
adb logcat | grep-e" ^.. myapp|^. MyActivity "  #use egrep without escape character
2. Filter unwanted output, blacklist


or using grep, use the same as above, plus a-v. For example, to filter the output of the tag for MyApp and myactivity:


ADB Logcat | Grep-v "^. myapp\|^. MyActivity "
adb logcat | grep-ve" ^.. myapp|^. MyActivity "  #use egrep without escape character
3. Show all outputs of the same process


Sometimes a program has more than one tag, you need to output the program (the same PID) of all the tag; using only tag filtering sometimes misses some error messages, and the general error message is the same PID as the program. or through grep, the idea is to first find the PID number according to the package name, and then match the PID. Write the shell script as follows, the parameter is the Java package name of the program (such as Com.android.media). View Source code Printing Help


1 #!/bin/bash
2 Packagename=$1
3 pid= ' adb shell PS | grep $packageName | awk ' {print $} '
4 ADB Logcat | grep--color=auto $pid
4. Show from current start


Logcat has a cache, if you only need to view the currently started log, you need to clear the previous.


ADB logcat-c && adb logcat
5. Filter log Files


Sometimes you need to parse the log file, filter the log file, or use grep. For example, the log file is Myapp.log, to match the output of tag MyApp and myactivity, and then output to Newmyapp.log:


Cat Myapp.log | grep "^. myapp\|^. MyActivity "> Newmyapp.log


Windows recommends using notepad++, a free and powerful notepad, to support regular expression lookup replacements. You can highlight matches or delete unwanted content.



The above technique mainly uses the grep, actually LOGCAT itself also has the filtering function, may filter the log according to the tag, the priority level, specifically please refer to the Android Official document Reading and Writing Logs. If you prefer to use the graphical interface, please refer to the logcat inside the using DDMS,DDMS can also be filtered.






Http://bbs.gfan.com/thread-1834459-1-1.html





Logcat detailed usage of Android
The Android log system provides the ability to record and view system debug information. Logs are recorded from a variety of software and some system buffers, and buffers can be viewed and used by the Logcat command.

Using the Logcat command, you can use the Logcat command to view the contents of the System log buffer:
[ADB] logcat [<option>] ... [<filter-spec>] ...
Please see the Listing of Logcat command Options, which have a detailed description of the Logcat commands.

You can also use the LOGCAT command on your computer or the remote ADB shell running on the emulator/device, or you can view the log output on your computer.
$ adb Logcat
You also use this:
# logcat Filter log output each output of the Android log information has a label and its priority.
The label of the log is a brief indication of the original information of the system part. (For example, "View" is to view the system's label).
Priority has the following concentrations, which are arranged smoothly from low to High:
V-verbose (Lowest priority)
D-debug
I-info
W-warning
E-error
F-fatal
S-silent (highest priority, on which nothing is ever printed)
In the first two columns when you run Logcat, you can see the LOGCAT tag list and priority level, which is the:<priority>/<tag>.

Here is an example of a logcat output, which has the precedence of I, the tag is activitymanage:

I/activitymanager (585): Starting activity:intent {action=android.intent.action ...}

In order for the log output to reflect the level of management, you can also use filters to control the log output, and filters can help you describe the system's label level.

Filter statements are described in the following format tag:priority ..., tag is a label, priority is the lowest level of reporting for the label. The priority of the log can be obtained from the above tag. You can write tag:priority multiple times in the filter.

These instructions are only to the blank end. Here is an example that shows support for all log information except those labeled "Activitymanager" and the priority "Info" above and labeled "MYAPP" and Priority "Debug" above. The priority report is the tag.

ADB logcat activitymanager:i Myapp:d *:s

The last element of the expression above *:s, is to set all the labels as "silent", all the logs are only shown with "View" and "MyApp", another use of *:s is to be able to ensure that the log output is in accordance with the filter description limit, Also let the filter output to the log as an item.

The following filter statements refer to log information that shows priority warning or higher:
ADB logcat *:w

If you run Logcat on your computer, you can also android_log_tags for environment variables: Enter a parameter to set the default filter, compared to the remote Adbshell side.
Export android_log_tags= "activitymanager:i Myapp:d *:s "

It is important to note that the Android_log_tags filter cannot output the log if it runs logcat through the remote shell or runs the emulator/device with the ADB shell logcat. The control log Output format log information includes many metadata fields including labels and precedence. You can modify the output format of the log so that a specific metadata field can be displayed. Information about the formatted output log can be obtained through the-v option.

Brief-display Priority/tag and PID of originating process (the default format).
Process-display PID only.
Tag-display the Priority/tag only.
Thread-display Process:thread and Priority/tag only.
Raw-display the raw log message with and no other metadata fields.
Time-display the date, invocation time, Priority/tag, and PID of the originating process.
Long-display all metadata fields and separate messages with a blank lines.

When Logcat is started, you can specify the output format with the-V option:
[ADB] logcat [-v <format>]

Here is the log format generated with thread:
ADB logcat-v Thread

Note that you only have the-V option to specify the output format options. View available log buffers The Android log system has a circular buffer, and not all log systems have a default loop buffer. In order to get the log information, you need to start logcat with the-B option. If you want to use a loop buffer, you need to see the remaining loop buffer period:

radio-View the information about the buffer.
events-view the buffer associated with the event.
main-viewing the primary log buffer

The-B option uses the method:
[ADB] logcat [-b <buffer>]

The following example shows how to view the log buffer containing radio and telephony information:
ADB logcat-b Radio View stdout and stderr by default, the Android system has stdout and stderr (System.out and System.err) output to/dev/null, running Dalvik V M in the process, there is a system that can back up the log files. In this case, the system uses stdout and stderr and the priority I to record the log information.

This method specifies the path to the output, stops the emulator/device running, and then remotely enters the log by using the SetProp command

$ adb shell stop
$ adb shell SetProp Log.redirect-stdio true
$ adb shell start

System until you close the simulator/device before the settings will remain, you can add/data/local.prop can use the emulator/device default settings logcat command list

The
Option Description
-B <buffer> loads a log buffer that can be used for viewing, such as event and radio. The default value is main. See Viewing alternative Log buffers for details.
-C clear the Log on the screen.
-D output the log to the screen.
-F <filename> Specifies the <filename> of the output log information, which is stdout by default.
-G outputs the specified log buffer and exits after output.
-n <count> Set the maximum number of logs <count>., the default value is 4 and needs to be used with the-r option.
-R <kbytes> output log per <kbytes>, default value is 16 and needs to be used with the-f option.
-S sets the default filter level to silent.
-v <format> Set the log input format, the default is the brief format, to know more supported formats, see controlling log Output format< /td>
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.