前言:Logcat—Android中一个命令行工具可以用于得到程序的log信息。在日常开发中,需要查看Logcat是在所难免的,换句话说是不可避免的。所以就需要掌握好Logcat命令行的使用,就可以在测试的电脑上一顿操作就可以发现导致Bug出现的问题了。
文章目录
运行命令窗口的方法:
1、可以使用window+R后输入cmd调出命令行窗口
2、直接在IDEA软件里使用Terminal(不推荐这个方法 既然你都有IDEA了,直接用里面自带的logcat更加省事)
一起来看一下Logcat命令行的介绍
C:\Users\Administrator>adb logcat -help
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 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'.
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).
Logcat命令行的使用
从上面的Logcat的介绍来看,Logcat的主要语法是
logcat [options] [filterspecs]
[]代表的是可选项,所以我们可以直接使用logcat命令直接输出最原生态的日志
其中包括日期时间,进程和线程的ID,日志的等级,类名等信息
那如果就是想抓完整的log到桌面上该如何做?只需要在后面加上" > 存储路径及文件名 "就可以了
结束抓取的话就Ctrl+C
两种方式都可以
adb logcat > desktop\log.txt
或者
adb shell logcat > C:\Users\Administrator\Desktop\log.txt
[options]中的-v的少部分用法
adb logcat -v threadtime和adb logcat 是一样的,所以threadtime是默认的格式
adb logcat -v time // 只显示日期、调用时间、优先级/标签和PID发出消息的进程
adb logcat -v brief // 只显示发出消息的进程的优先级/标记和PID
这两个差不多
adb logcat -v process // 只显示PID
adb logcat -v raw // 显示原始日志消息,不包含其他元数据字段
[filterspecs]是一系列的 “<tag>[:priority]”
其中<tag>是日志组件标签(或*表示所有标签),优先级为:
V Verbose(<tag>的默认值)
D Debug(默认为 *)
I Info
W Warn
E Error
F Fatal
S Silent(抑制所有输出)
所以根据上面的介绍 如果输出Info级及其以下的日志:
adb logcat *:I
如果输出Error级及其以下的日志:
adb logcat *:E
给日志加点颜色
看到这里 日志都是一片灰白灰白的字,可以给log加点颜色看看就可以使用这个命令,可以搭配其他指令一起使用。
其中白色代表的是Verbose等级的,蓝色是Debug,绿色是Info,橙色是Warning,红色是Error
adb logcat -v color
过滤想要查看的log关键字
// 过滤带有“connect”的log
adb logcat | grep connect
// 过滤带有“connect”的log,不区分大小写
adb logcat | grep -i connect
// 全词匹配
adb logcat | grep -w connect
更多关于grep的用法:
–color=auto 或者 –color:表示对匹配到的文本着色显示-i:在搜索的时候忽略大小写
-n:显示结果所在行号
-c:统计匹配到的行数,注意,是匹配到的总行数,不是匹配到的次数
-o:只显示符合条件的字符串,但是不整行显示,每个符合条件的字符串单独显示一行
-v:输出不带关键字的行(反向查询,反向匹配)
-w:匹配整个单词,如果是字符串中包含这个单词,则不作匹配
-Ax:在输出的时候包含结果所在行之后的指定行数,这里指之后的x行,A:after
-Bx:在输出的时候包含结果所在行之前的指定行数,这里指之前的x行,B:before
-Cx:在输出的时候包含结果所在行之前和之后的指定行数,这里指之前和之后的x行,C:context
-e:实现多个选项的匹配,逻辑or关系
-q:静默模式,不输出任何信息,当我们只关心有没有匹配到,却不关心匹配到什么内容时,我们可以使用此命令,然后,使用”echo $?”查看是否匹配到,0表示匹配到,1表示没有匹配到。
-P:表示使用兼容perl的正则引擎。
-E:使用扩展正则表达式,而不是基本正则表达式,在使用”-E”选项时,相当于使用egrep。
组合使用
抓取等级为Warning及以下的log,并使用颜色区分等级,并且过滤带有“connect”字符串的log并且对文本着色
adb logcat -v color *:W | grep -i connect --color
最后
更多使用方法 需要结合自身所需要的情况进行logcat和grep 一起用起来!