In Android development, we can always see that the program's log content is filled with screens, and information that is truly meaningful to developers is submerged in the torrent, leaving developers at a loss and seriously affecting development efficiency. This article specifically introduces several methods to filter adb logcat output in the shell command line.
1. Only display the required output (white list)
The most convenient thing is of course to use grep to filter through the pipeline, so that grep can be used to match the powerful regular expression. Simple match a string in a line, 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, please see grep Help.
To be more advanced, you can use grep's regular expression matching. For example, the previous example matches MyApp at any position in a row, which can be set to match only tags. The default log output is as follows. If the corresponding expression in the output format has been modified, it must also be modified.
I/CacheService( 665): Preparing DiskCache for all thumbnails.
It can be seen that tag is the third character at the beginning of a line, and write the expression based on this point:
adb logcat | grep "^..MyApp"
According to this format, you can also set the log of only a certain priority to display, and then match the first character at the beginning of the line. For example, only the output of the Error level tag is MyApp:
adb logcat | grep "^"
Of course, it can also match multiple match expressions. Use | to split multiple matching expressions and add escape characters. For example to match the output of tags to MyApp and MyActivity:
adb logcat | grep "^..MyApp\|^..MyActivity"
adb logcat | grep -E "^..MyApp|^..MyActivity" #Use egrep without escaping characters
2. Filter unwanted output (blacklist)
Or use grep, the usage is the same as above, just add a -v. For example, to filter the output of tags to MyApp and MyActivity:
adb logcat | grep -v "^..MyApp\|^..MyActivity"
adb logcat | grep -vE "^..MyApp|^..MyActivity" #Use egrep without escaping characters
3. Display all outputs of the same process
Sometimes there are multiple tags in a program, and all tags of the program (same PID) need to be output; using tags only to filter can sometimes miss some error information, and the general error information is also the same PID as the program. Or implement it through grep. The idea is to first find the pid number based on the package name and then match the pid. Written as shell script as follows, the parameters are the java package name of the program (such as ).
#!/bin/bash
packageName=$1
pid=`adb shell ps | grep $packageName | awk ‘{print $2}'`
adb logcat | grep –color=auto $pid
4. Display from the current
logcat has a cache. If you only need to view the current log, you need to clear the previous one.
adb logcat -c && adb logcat
5. Filter log files
Sometimes you need to analyze the log file, filter the log file or use grep. For example, the log file is , to match the output of tag MyApp and MyActivity, and then output to:
cat | grep "^..MyApp\|^..MyActivity" >
Notepad++ is recommended for Windows, a free and powerful notepad that supports regular expression search replacement. You can highlight matching content or delete unnecessary content.
The above techniques mainly use grep. In fact, logcat itself also has a filtering function, which can filter logs according to tags and priorities. For details, please refer to the official Android document Reading and Writing Logs. If you like to use a graphical interface, please refer to Using DDMS. The logcat in DDMS can also be filtered.
The above is a compilation of the information on the method of filtering adb logcat output in the Android shell command line. We will continue to add it later. Thank you for your support for this site!