Android的logcat日志详解

打印 上一主题 下一主题

主题 1023|帖子 1023|积分 3069

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

x
Android log体系


logcat先容

logcatandroid中的一个下令行工具,可以用于得到程序的log信息。下面先容 adb logcat中的详细参数下令以及如何才气高效的打印日志,或把日志生存到我们指定的位置。
可以输入 adb logcat --help,查看一下一些简单的数据格式:
  1. adb logcat --help
复制代码
输出结果:
  1. Usage: logcat [options] [filterspecs]
  2. General options:
  3.   -b, --buffer=<buffer>       Request alternate ring buffer(s):
  4.                                 main system radio events crash default all
  5.                               Additionally, 'kernel' for userdebug and eng builds, and
  6.                               'security' for Device Owner installations.
  7.                               Multiple -b parameters or comma separated list of buffers are
  8.                               allowed. Buffers are interleaved.
  9.                               Default -b main,system,crash,kernel.
  10.   -L, --last                  Dump logs from prior to last reboot from pstore.
  11.   -c, --clear                 Clear (flush) the entire log and exit.
  12.                               if -f is specified, clear the specified file and its related rotated
  13.                               log files instead.
  14.                               if -L is specified, clear pstore log instead.
  15.   -d                          Dump the log and then exit (don't block).
  16.   --pid=<pid>                 Only print logs from the given pid.
  17.   --wrap                      Sleep for 2 hours or when buffer about to wrap whichever
  18.                               comes first. Improves efficiency of polling by providing
  19.                               an about-to-wrap wakeup.
  20. Formatting:
  21.   -v, --format=<format>       Sets log print format verb and adverbs, where <format> is one of:
  22.                                 brief help long process raw tag thread threadtime time
  23.                               Modifying adverbs can be added:
  24.                                 color descriptive epoch monotonic printable uid usec UTC year zone
  25.                               Multiple -v parameters or comma separated list of format and format
  26.                               modifiers are allowed.
  27.   -D, --dividers              Print dividers between each log buffer.
  28.   -B, --binary                Output the log in binary.
  29. Outfile files:
  30.   -f, --file=<file>           Log to file instead of stdout.
  31.   -r, --rotate-kbytes=<n>     Rotate log every <n> kbytes. Requires -f option.
  32.   -n, --rotate-count=<count>  Sets max number of rotated logs to <count>, default 4.
  33.   --id=<id>                   If the signature <id> for logging to file changes, then clear the
  34.                               associated files and continue.
  35. Logd control:
  36. These options send a control message to the logd daemon on device, print its return message if
  37. applicable, then exit. They are incompatible with -L, as these attributes do not apply to pstore.
  38.   -g, --buffer-size           Get the size of the ring buffers within logd.
  39.   -G, --buffer-size=<size>    Set size of a ring buffer in logd. May suffix with K or M.
  40.                               This can individually control each buffer's size with -b.
  41.   -S, --statistics            Output statistics.
  42.                               --pid can be used to provide pid specific stats.
  43.   -p, --prune                 Print prune rules. Each rule is specified as UID, UID/PID or /PID. A
  44.                               '~' prefix indicates that elements matching the rule should be pruned
  45.                               with higher priority otherwise they're pruned with lower priority. All
  46.                               other pruning activity is oldest first. Special case ~! represents an
  47.                               automatic pruning for the noisiest UID as determined by the current
  48.                               statistics.  Special case ~1000/! represents pruning of the worst PID
  49.                               within AID_SYSTEM when AID_SYSTEM is the noisiest UID.
  50.   -P, --prune='<list> ...'    Set prune rules, using same format as listed above. Must be quoted.
  51. Filtering:
  52.   -s                          Set default filter to silent. Equivalent to filterspec '*:S'
  53.   -e, --regex=<expr>          Only print lines where the log message matches <expr> where <expr> is
  54.                               an ECMAScript regular expression.
  55.   -m, --max-count=<count>     Quit after printing <count> lines. This is meant to be paired with
  56.                               --regex, but will work on its own.
  57.   --print                     This option is only applicable when --regex is set and only useful if
  58.                               --max-count is also provided.
  59.                               With --print, logcat will print all messages even if they do not
  60.                               match the regex. Logcat will quit after printing the max-count number
  61.                               of lines that match the regex.
  62.   -t <count>                  Print only the most recent <count> lines (implies -d).
  63.   -t '<time>'                 Print the lines since specified time (implies -d).
  64.   -T <count>                  Print only the most recent <count> lines (does not imply -d).
  65.   -T '<time>'                 Print the lines since specified time (not imply -d).
  66.                               count is pure numerical, time is 'MM-DD hh:mm:ss.mmm...'
  67.                               'YYYY-MM-DD hh:mm:ss.mmm...' or 'sssss.mmm...' format.
  68.   --uid=<uids>                Only display log messages from UIDs present in the comma separate list
  69.                               <uids>. No name look-up is performed, so UIDs must be provided as
  70.                               numeric values. This option is only useful for the 'root', 'log', and
  71.                               'system' users since only those users can view logs from other users.
  72. filterspecs are a series of
  73.   <tag>[:priority]
  74. where <tag> is a log component tag (or * for all) and priority is:
  75.   V    Verbose (default for <tag>)
  76.   D    Debug (default for '*')
  77.   I    Info
  78.   W    Warn
  79.   E    Error
  80.   F    Fatal
  81.   S    Silent (suppress all output)
  82. '*' by itself means '*:D' and <tag> by itself means <tag>:V.
  83. If no '*' filterspec or -s on command line, all filter defaults to '*:V'.
  84. eg: '*:S <tag>' prints only <tag>, '<tag>:S' suppresses all <tag> log messages.
  85. If not specified on the command line, filterspec is set from ANDROID_LOG_TAGS.
  86. If not specified with -v on command line, format is set from ANDROID_PRINTF_LOG
  87. or defaults to "threadtime"
复制代码
log类是一个日志类,可以在代码中利用logcat打印出消息。常见的日志记载方法包罗。
方法
形貌
v(String,String) (vervbose)
显示全部信息
d(String,String)(debug)
显示调试信息
i(String,String)(information)
显示⼀般信息
w(String,String)(waning)
显示警告信息
e(String,String)(error)
显示错误信息
比方:
  1. // 开发过程中获取 log
  2. Log.i("MyActivity","MyClass.getView() - get item number"+position);
  3. //adb 获取 log
  4. adb logcat
  5. adb logcat 输出的⽇志格式如下:
  6. I/ActivityManager( 1754): Waited long enough for: ServiceRecord{2b24178c u0
  7. com.google.android.gms/.checkin.CheckinService}
复制代码
logcat下令格式

语法格式:
  1. [adb] logcat [] … [ ] …
  2. PC 端使⽤: adb logcat
  3. shell 模式下使⽤: logcat
复制代码
logcat缓冲区

缓冲区先容

Android 日志体系为日志消息保留了多个环形缓冲区,但并非多有的日志消息都会发送到默认的环形缓冲区。这里可以采用 logcat -b 下令查看设备的其他缓冲区:
缓冲区
形貌
举例
radio
输出通讯体系的日志,包罗无线装置/电话相关消息
logcat -b radio
events
输出event模块的日志
logcat -b events
main
主日志缓冲区(默认),不包罗体系和瓦解日志消息
logcat -b main
system
输出体系日志
logcat -b system
crash
输出瓦解日志
logcat -b crash
all
输出所有缓冲区日志
logcat -b all
default
输出main、system、crash缓冲区日志
logcat -b default
缓冲区模型


logcat下令参数

参数
寄义
举例
-b
加载⼀个可使⽤的⽇志缓冲区供查看,⽐如 event和 radio 。默认值是 main
logcat -b system
-B
以二进制文件情势输出日志

-c
清除缓冲区中的全部⽇志并退出(清除完后可以使⽤ -g 查看缓冲区)默认清除 main、system 和 crash
logcat -c;logcat -b all -c
-d
将缓冲区的 log 转存到屏幕中然后退出
logcat -d > log.txt
-f <file>
将 log 输出到指定的⽂件中 < ⽂件名 >. 默以为标准输出( stdout )
logcat -f sdcard/log.txt
-g
打印⽇志缓冲区的巨细并退出
logcat -g
-G
设置日志环形缓冲区的巨细,可以在末端处添加 K 或 M
logcat -G 2M
-n <count>
设置⽇志的最⼤数⽬,默认值是 4 ,需要和 -r 选项⼀起使⽤

-r <kbytes>
每时输出⽇志,默认值是 16 ,需要和 -f 选项⼀起使⽤
logcat -f sdcard/log.txt -r 1
-s
输出指定 tag 的日志,相当于过滤器表达式 *:S
logcat -s tag
-S
在输出中包罗统计信息,以识别和定位日志垃圾信息发送者

-v <format>
设置输出格式的⽇志消息。默认是短暂的格式。⽀持的格式列表
logcat -v threadtime
-m <count>
输出 行后退出
logcat -m 5
-D
输出各个日志缓冲区之间的分隔线
logcat -D …
-e <expr>
输出正则匹配的日志消息
logcat -e [匹配数据] -m 5
-t <count>
仅输出最新的行数,此选项包罗 -d 功能
logcat -t 5
-t '<time>'
输出自指定时间以来的最新行,此选项包罗 -d 功能
logcat -t ‘01-26 20:52:41.820’
--pid=<pid>
仅输出来自给定 PID 的日志
logcat --pid=4321
   ⼀般⻓时间输出 log 的话发起 -f , -n , -r 三个参数连⽤,如许当⼀个⽂件⽇志输出满了之后可以⻢上在另⼀个中进⾏输出。
  Android 日志体系为日志消息保留了多个环形缓冲区,但并非多有的日志消息都会发送到默认的环形缓冲区。
  logcat -b 是 logcat 下令的一个选项,用于指定查看日志缓冲区。你可以用它来查看差异范例的日志缓冲区。下面是基本用法:
  1. logcat -b events|grep -E "am_proc_start|input_focus.*entering"
复制代码
基本下令格式:

  1. shCopy Code
  2. adb logcat -b <buffer>
复制代码
选项说明:

   

  • main: 默认缓冲区,包罗大多数应用程序和体系日志。
  • system: 体系日志缓冲区,包罗体系服务和硬件相关日志。
  • events: 事件日志缓冲区,记载体系事件和调试信息。
  • crash: 瓦解日志缓冲区,记载应用瓦解的信息。
  示例用法:



  • 查看主缓冲区日志:
  1. shCopy Code
  2. adb logcat -b main
复制代码


  • 查看体系缓冲区日志:
  1. shCopy Code
  2. adb logcat -b system
复制代码


  • 查看事件缓冲区日志:
  1. shCopy Code
  2. adb logcat -b events
复制代码


  • 查看瓦解缓冲区日志:
  1. shCopy Code
  2. adb logcat -b crash
复制代码
这些选项资助你根据需要过滤和查看差异范例的日志数据。
logcat格式化输出

参数说明

⽇志消息包罗⼀个元数据字段,除了标签和优先级,您可以修改输出显示⼀个特定的元数据字段格式的消息。为此,您使⽤-v 选项来指定⼀个⽀持的输出格式。⼀下为⽀持的格式:
格式
寄义
brief
显示优先级 / 标志和过程的 PID 发出的消息(默认格式)
process
只显示PID
tag
只显示优先级/标志
raw
显示原始的⽇志消息,没有其他元数据字段
time
调⽤显示⽇期、时间、优先级 / 标签和过程的 PID 发出消息
thread
旧版格式,显示优先级、PID 以及发出消息的线程的 TID
threadtime
调⽤显示⽇期、时间、优先级、标签遗迹 PID TID线程发出的消息
long
显示所有元数据字段与空⽩⾏和单独的消息
color
利用差异的颜色来显示每个优先级
descriptive
显示日志缓冲区事件说明。此修饰符仅影响事件日志缓冲区消息,不会对其他非二进制文件缓冲区产生任何影响
epoch
显示自 1970 年 1 月 1 日以来的时间(以秒为单元)
uid
如果访问控制允许,则显示 UID 或记载的进程的 Android ID
usec
显示精确到微秒的时间
UTC
显示 UTC 时间
year
将年份添加到显示的时间
zone
将本地时区添加到显示的时间
当 logcat 开始,指定想要输出格式 -v 选项:
  1. [adb] logcat [-v ]
  2. adb logcat –v thread
复制代码
只能指定⼀个输出格式-v。
logcat优先级

按日志级别过滤日志:
  1. adb logcat <tag>[:priority]
复制代码
  tag 体现标签;priority 体现输出级别;
  日志默认级别是 V,如果错误日志我们选择 E 就可以,Android 的日志分为如下几个优先级(priority):
选项
形貌
举例
V
–Verbose(最低优先级)
adb logcat *:V
D
– Debug
adb logcat *
I
– Info
adb logcat *:I
W
– Warning
adb logcat *:W
E
– Error
adb logcat *:E
F
– Fatal
adb logcat *:F
S
– Silent
adb logcat *:S
   *可以是某个tag,如果没有指明,就体现所有。

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

天空闲话

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表