We can always see the log log content of the program is full of screens in the Android development, and the information that really makes sense to the developer is submerged in the torrent, so that the developers are at a loss and seriously affect the development efficiency. This article describes several methods of filtering adb logcat output in the shell command line.
1. Show only 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 #忽略大小写.
ADB Logcat | grep--color=auto-i MyApp #设置匹配字符串颜色. 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 "#使用 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 "#使用 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).
#!/bin/bash
Packagename=$1
pid= ' adb shell PS | grep $packageName | awk ' {print $} '
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.
Several methods of filtering adb logcat output in the shell command line