adb logcat查看输出日志的技巧备忘

前两天把免费撸电信的C8815给刷成了MIUI,感觉很不错,装了幸运破解器,但发现闪退,就连电脑看了下日志,发现没装Android Studio来调试真心难受,可惜电脑太渣跑不动Android Studio,只用adb来看了.

使用帮助

首先用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 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'.
                  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"

日志等级

日志分为7个级别,但实际上最后一个S是屏蔽日志输出的意思,因此有效的级别是6个.

  V    Verbose (default for <tag>)
  D    Debug (default for '*')
  I    Info
  W    Warn
  E    Error
  F    Fatal
  S    Silent (suppress all output)

过滤日志

adb logcat <tag>[:priority] <tag>[:priority] ...

tag 是输出日志的标签,通常是应用名,priority则是日志输出的等级.可以组合多个.

查看所有等级和所有应用输出的日志的命令直接就是:adb logcat

查看所有应用的Warn及更高等级的日志命令:adb logcat *:W

只查看含有特定标签的Debug命令,如包含Test标签: adb logcat Test:D *:S

查看特定进程的日志,则首先确定运行的应用进程pid,可以使用命令adb shell进入shell模式,然后使用ps -ef | grep xxx来过滤出pid,然后退出shell模式,使用命令adb logcat --pid=xxx查看所有该应用的日志,也可以加上Level过滤指定等级的日志,比如:adb logcat *:E --pid=xxx

当连接了多个设备时,先使用adb devices获取设备序列号,然后使用-s <serial number>参数指定设备的序列号,比如设备序列号为7512874,则查看日志的命令可以是:adb -s 7512874 logcat *:E --pid=xxx

格式化输出

使用-v命令可以格式化输出,有以下几种格式输出:
1. brief 默认格式

<priority>/<tag>(<pid>): <message>

  1. process

    <priority>(<pid>) <message>

  2. tag

    <priority>/<tag>: <message>

  3. raw

    <message>

  4. time

    <datetime> <priority>/<tag>(<pid>): <message>

  5. threadtime

    <datetime> <pid> <tid> <priority> <tag>: <message>

  6. long

    <datetime> <pid>:<tid> <priority>/<tag>