For a while I've been using
logcat command line tools to check
Android logs. Usually, the tags of my app consist of a common prefix and the
name of different sub-components (I guess that's also what most apps do). And I
have about a dozen of such tags.
logcat, however, does not support filtering
tags using regular expressions, which is a pain! After suffering for a long
time, I finally decide to tackle this.
Logcat Tag Filters
logcat options can be found in the official document,
which also contains a brief explanation of
logcat filter format.
Basically, you provide a series of
logcat filters, each with the format
TAG must be the exact tags you want to filter, and
LEVEL can be one of the characters listed in the document. So if you have a
bunch of similar tags, such as
MyApp-Task2, etc., you'll have
to specify them all in full name. Although you can save a few key strokes by setting
ANDROID_LOG_TAGS environment variable, it still only solves part of the
Note that the order of the filters matters. In short,
logcat will look at the
filters from left to right, and use the first one that matches the tag of
log line. For example, if you use
MyApp:V *:S, then only the log lines with
MyApp will be printed, other log lines will be suppressed by the
filter. However, if you use
*:S MyApp:V, then no log lines will be printed,
because the first filter,
*:S matches all log line tags thus all log lines are
suppressed by this filter. For details, please refer to the
android_log_shouldPrintLine function in this file.
My Logcat Wrapper
We can make
logcat support regular expression tag filters by two approaches.
One is modifying
logcat source code in AOSP tree and build a new
binary that support RE. Another approach is to filter the tags "offline" in the
host PCs where you run
adb logcat command, i.e., a
I adopted the second approach, since the first one has a few drawbacks:
- You'll have to match RE in cpp, which I assume is not quite enjoyable.
- You'll have to cross-compile the
logcatbinary, which requires you to setup the whole AOSP develop environment.
- For each device that you want to run
logcaton, you'll have to replace the
So here is my wrapper works. It calls
adb logcat command without any filters,
to get all the log lines. Then it parses the output log lines, and only prints
the lines whose tag matches the regular expression provided. It supports basic
logcat options, such as
-g, etc. by just piping those options to
logcat. It processes the log filters in the same order as
does to be as close as the original
The idea is that you just run the wrapper in the save way you would as
and it just does the magic RE tag filtering for you. You can find this tool on
my github repo.
You can directly print those log lines to console. I personally prefer to redirect them to a temporary file and use vim to view it, which give me features like incremental highlight search, etc. There is a sweet recipe which tells vim to automatically refresh the buffer when it's modified outside. This is a perfect fit in viewing log files.