天空闲话 发表于 2024-9-7 11:40:13

Android的logcat日志详解

Android log体系

https://i-blog.csdnimg.cn/direct/89c7709eadf6481e8351bef52b2a2b2a.png
logcat先容

logcat是android中的一个下令行工具,可以用于得到程序的log信息。下面先容 adb logcat中的详细参数下令以及如何才气高效的打印日志,或把日志生存到我们指定的位置。
可以输入 adb logcat --help,查看一下一些简单的数据格式:
adb logcat --help 输出结果:
Usage: logcat

General options:
-b, --buffer=<buffer>       Request alternate ring buffer(s):
                              main system radio events crash default 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 are interleaved.
                              Default -b main,system,crash,kernel.
-L, --last                  Dump logs from prior to last reboot from pstore.
-c, --clear               Clear (flush) the entire log and exit.
                              if -f is specified, clear the specified file and its related rotated
                              log files instead.
                              if -L is specified, clear pstore log instead.
-d                        Dump the log and then exit (don't block).
--pid=<pid>               Only print 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.

Formatting:
-v, --format=<format>       Sets log print format verb and adverbs, where <format> is one of:
                              brief help long process raw tag thread threadtime time
                              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.
-B, --binary                Output the log in binary.

Outfile files:
-f, --file=<file>         Log to file instead of stdout.
-r, --rotate-kbytes=<n>   Rotate log every <n> kbytes. Requires -f option.
-n, --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
                              associated files and continue.

Logd control:
These options send a control message to the logd daemon on device, print its return message if
applicable, then exit. They are incompatible with -L, as these attributes do not apply to pstore.
-g, --buffer-size         Get the size of the ring buffers within logd.
-G, --buffer-size=<size>    Set size of a ring buffer in logd. May suffix with K or M.
                              This can individually control each buffer's size with -b.
-S, --statistics            Output statistics.
                              --pid can be used to provide pid specific stats.
-p, --prune               Print prune rules. Each rule is specified as UID, UID/PID or /PID. A
                              '~' prefix indicates that elements matching the rule should be pruned
                              with higher priority otherwise they're pruned with lower priority. All
                              other pruning activity is oldest first. Special case ~! represents an
                              automatic pruning for the noisiest UID as determined by the current
                              statistics.Special case ~1000/! represents pruning of the worst PID
                              within AID_SYSTEM when AID_SYSTEM is the noisiest UID.
-P, --prune='<list> ...'    Set prune rules, using same format as listed above. Must be quoted.

Filtering:
-s                        Set default filter to silent. Equivalent to filterspec '*:S'
-e, --regex=<expr>          Only print lines where the log message matches <expr> where <expr> is
                              an ECMAScript regular expression.
-m, --max-count=<count>   Quit after printing <count> lines. This is meant to be paired with
                              --regex, but will work on its own.
--print                     This option is only applicable when --regex is set and only useful if
                              --max-count is also provided.
                              With --print, logcat will print all messages even if they do not
                              match the regex. Logcat will quit after printing the max-count number
                              of lines that match the regex.
-t <count>                  Print only the most recent <count> lines (implies -d).
-t '<time>'               Print the lines since specified time (implies -d).
-T <count>                  Print only the most recent <count> lines (does not imply -d).
-T '<time>'               Print the 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.
--uid=<uids>                Only display log messages from UIDs present in the comma separate list
                              <uids>. No name look-up is performed, so UIDs must be provided as
                              numeric values. This option is only useful for the 'root', 'log', and
                              'system' users since only those users can view logs from other users.

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" log类是一个日志类,可以在代码中利用logcat打印出消息。常见的日志记载方法包罗。
方法
形貌
v(String,String) (vervbose)
显示全部信息
d(String,String)(debug)
显示调试信息
i(String,String)(information)
显示⼀般信息
w(String,String)(waning)
显示警告信息
e(String,String)(error)
显示错误信息
比方:
// 开发过程中获取 log
Log.i("MyActivity","MyClass.getView() - get item number"+position);
//adb 获取 log
adb logcat
adb logcat 输出的⽇志格式如下:
I/ActivityManager( 1754): Waited long enough for: ServiceRecord{2b24178c u0
com.google.android.gms/.checkin.CheckinService} logcat下令格式

语法格式:
logcat [] … [ ] …
PC 端使⽤: adb logcat
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
缓冲区模型

https://i-blog.csdnimg.cn/direct/17234477b67742a795425116b514ab45.png
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 下令的一个选项,用于指定查看日志缓冲区。你可以用它来查看差异范例的日志缓冲区。下面是基本用法:
logcat -b events|grep -E "am_proc_start|input_focus.*entering" 基本下令格式:

shCopy Code
adb logcat -b <buffer> 选项说明:

   

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



[*] 查看主缓冲区日志:
shCopy Code
adb logcat -b main

[*] 查看体系缓冲区日志:
shCopy Code
adb logcat -b system

[*] 查看事件缓冲区日志:
shCopy Code
adb logcat -b events

[*] 查看瓦解缓冲区日志:
shCopy Code
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 选项:
logcat [-v ]
adb logcat –v thread 只能指定⼀个输出格式-v。
logcat优先级

按日志级别过滤日志:
adb logcat <tag>[:priority]   tag 体现标签;priority 体现输出级别;
日志默认级别是 V,如果错误日志我们选择 E 就可以,Android 的日志分为如下几个优先级(priority):
选项
形貌
举例
V
–Verbose(最低优先级)
adb logcat *:V
D
– Debug
adb logcat *:D
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企服之家,中国第一个企服评测及商务社交产业平台。
页: [1]
查看完整版本: Android的logcat日志详解