Android Adb 命令之 logcat

本文记录了一些 Logcat 常用的命令。比如过滤日志,输出到文件等。

1
2
3
$ adb version
Android Debug Bridge version 1.0.41
Version 31.0.3-7562133
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
$ adb logcat -h
Usage: logcat [options] [filterspecs]
options include:
-s Set default filter to silent. Equivalent to filterspec '*:S'
-f <file>, --file=<file> Log to file. Default is stdout
-r <kbytes>, --rotate-kbytes=<kbytes>
Rotate log every kbytes. Requires -f option
-n <count>, --rotate-count=<count>
Sets max number of rotated logs to <count>, default 4
--id=<id> If the signature id for logging to file changes, then clear
the fileset and continue
-v <format>, --format=<format>
Sets log print format verb and adverbs, where <format> is:
brief help long process raw tag thread threadtime time
and individually flagged modifying adverbs can be added:
color descriptive epoch monotonic printable uid
usec UTC year zone
Multiple -v parameters or comma separated list of format and
format modifiers are allowed.
-D, --dividers Print dividers between each log buffer
-c, --clear Clear (flush) the entire log and exit
if Log to File specified, clear fileset instead
-d Dump the log and then exit (don't block)
-e <expr>, --regex=<expr>
Only print lines where the log message matches <expr>
where <expr> is a Perl-compatible regular expression
-m <count>, --max-count=<count>
Quit after printing <count> lines. This is meant to be
paired with --regex, but will work on its own.
--print Paired with --regex and --max-count to let content bypass
regex filter but still stop at number of matches.
-t <count> Print only the most recent <count> lines (implies -d)
-t '<time>' Print most recent lines since specified time (implies -d)
-T <count> Print only the most recent <count> lines (does not imply -d)
-T '<time>' Print most recent lines since specified time (not imply -d)
count is pure numerical, time is 'MM-DD hh:mm:ss.mmm...'
'YYYY-MM-DD hh:mm:ss.mmm...' or 'sssss.mmm...' format
-g, --buffer-size Get the size of the ring buffer.
-G <size>, --buffer-size=<size>
Set size of log ring buffer, may suffix with K or M.
-L, --last Dump logs from prior to last reboot
-b <buffer>, --buffer=<buffer> Request alternate ring buffer, 'main',
'system', 'radio', 'events', 'crash', 'default' or 'all'.
Additionally, 'kernel' for userdebug and eng builds, and
'security' for Device Owner installations.
Multiple -b parameters or comma separated list of buffers are
allowed. Buffers interleaved. Default -b main,system,crash.
-B, --binary Output the log in binary.
-S, --statistics Output statistics.
-p, --prune Print prune white and ~black list. Service is specified as
UID, UID/PID or /PID. Weighed for quicker pruning if prefix
with ~, otherwise weighed for longevity if unadorned. All
other pruning activity is oldest first. Special case ~!
represents an automatic quicker pruning for the noisiest
UID as determined by the current statistics.
-P '<list> ...', --prune='<list> ...'
Set prune white and ~black list, using same format as
listed above. Must be quoted.
--pid=<pid> Only prints logs from the given pid.
--wrap Sleep for 2 hours or when buffer about to wrap whichever
comes first. Improves efficiency of polling by providing
an about-to-wrap wakeup.

filterspecs are a series of
<tag>[:priority]

where <tag> is a log component tag (or * for all) and priority is:
V Verbose (default for <tag>)
D Debug (default for '*')
I Info
W Warn
E Error
F Fatal
S Silent (suppress all output)

'*' by itself means '*:D' and <tag> by itself means <tag>:V.
If no '*' filterspec or -s on command line, all filter defaults to '*:V'.
eg: '*:S <tag>' prints only <tag>, '<tag>:S' suppresses all <tag> log messages.

If not specified on the command line, filterspec is set from ANDROID_LOG_TAGS.

If not specified with -v on command line, format is set from ANDROID_PRINTF_LOG
or defaults to "threadtime"

-v <format>, --format=<format> options:
Sets log print format verb and adverbs, where <format> is:
brief long process raw tag thread threadtime time
and individually flagged modifying adverbs can be added:
color descriptive epoch monotonic printable uid usec UTC year zone

Single format verbs:
brief — Display priority/tag and PID of the process issuing the message.
long — Display all metadata fields, separate messages with blank lines.
process — Display PID only.
raw — Display the raw log message, with no other metadata fields.
tag — Display the priority/tag only.
thread — Display priority, PID and TID of process issuing the message.
threadtime — Display the date, invocation time, priority, tag, and the PID
and TID of the thread issuing the message. (the default format).
time — Display the date, invocation time, priority/tag, and PID of the
process issuing the message.

Adverb modifiers can be used in combination:
color — Display in highlighted color to match priority. i.e. VERBOSE
DEBUG INFO WARNING ERROR FATAL
descriptive — events logs only, descriptions from event-log-tags database.
epoch — Display time as seconds since Jan 1 1970.
monotonic — Display time as cpu seconds since last boot.
printable — Ensure that any binary logging content is escaped.
uid — If permitted, display the UID or Android ID of logged process.
usec — Display time down the microsecond precision.
UTC — Display time as UTC.
year — Add the year to the displayed time.
zone — Add the local timezone to the displayed time.
"<zone>" — Print using this public named timezone (experimental).

日志分为如下几个优先级:

  • V —— Verbose(默认值,最低,输出得最多)
  • D —— Debug
  • I —— Info
  • W —— Warning
  • E —— Error
  • F —— Fatal
  • S —— Silent(最高,啥也不输出)

日志格式

选项 示例
brief W/WindowManager( 1193): Unable to start animation
long [ 11-15 11:19:48.473 1193: 1213 W/WindowManager ] Unable to start animation
process W( 1193) Unable to start animation. (WindowManager)
raw Unable to start animation
tag W/WindowManager: Unable to start animation
thread W( 1193: 1213) Unable to start animation
threadtime 11-15 11:19:48.473 1193 1213 W WindowManager: Unable to start animation
time 11-15 11:19:48.473 W/WindowManager( 1193): Unable to start animation

filterspecs 采用 <tag>[:priority] 格式,可指定多个。其中 tag 指示过滤的标记,priority 指示可针对该标记报告的最低优先级。

adb logcat WifiConnectivityManager:D WindowManager:V *:S 表示输出 WifiConnectivityManager 的 Debug 以上级别日志和 WindowManager 的 Verbose 以上级别日志,及其它 tag 的 Silent 级别日志(即屏蔽其它 tag 日志)。

  • 清空日志
    adb logcat -c
  • 使用 long 格式化日志
    adb logcat -v time
  • 过滤 WindowManager 日志
    adb logcat -s WindowManager:V

参考

[1] logcat Command-line Tool