Android的logcat日志详解

2024-09-07 19:20
文章标签 android 日志 详解 logcat

本文主要是介绍Android的logcat日志详解,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Android log系统

logcat介绍

logcatandroid中的一个命令行工具,可以用于得到程序的log信息。下面介绍 adb logcat中的详细参数命令以及如何才能高效的打印日志,或把日志保存到我们指定的位置。

可以输入 adb logcat --help,查看一下一些简单的数据格式:

adb logcat --help

输出结果:

Usage: logcat [options] [filterspecs]General options:-b, --buffer=<buffer>       Request alternate ring buffer(s):main system radio events crash default allAdditionally, 'kernel' for userdebug and eng builds, and'security' for Device Owner installations.Multiple -b parameters or comma separated list of buffers areallowed. 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 rotatedlog 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 whichevercomes first. Improves efficiency of polling by providingan 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 timeModifying adverbs can be added:color descriptive epoch monotonic printable uid usec UTC year zoneMultiple -v parameters or comma separated list of format and formatmodifiers 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 theassociated files and continue.Logd control:These options send a control message to the logd daemon on device, print its return message ifapplicable, 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 prunedwith higher priority otherwise they're pruned with lower priority. Allother pruning activity is oldest first. Special case ~! represents anautomatic pruning for the noisiest UID as determined by the currentstatistics.  Special case ~1000/! represents pruning of the worst PIDwithin 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> isan 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 notmatch the regex. Logcat will quit after printing the max-count numberof 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 asnumeric 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    InfoW    WarnE    ErrorF    FatalS    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命令格式

语法格式:

[adb] 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

缓冲区模型

logcat命令参数

参数

含义

举例

-b

加载⼀个可使⽤的⽇志缓冲区供查看,⽐如 event和 radio 。默认值是 main

logcat -b system

-B

以二进制文件形式输出日志

-c

清除缓冲区中的全部⽇志并退出(清除完后可以使⽤ -g 查看缓冲区)默认清除 mainsystemcrash

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

设置日志环形缓冲区的大小,可以在结尾处添加 KM

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 -blogcat 命令的一个选项,用于指定查看日志缓冲区。你可以用它来查看不同类型的日志缓冲区。下面是基本用法:

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 选项:

[adb] 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,如果没有指明,就表示所有。

这篇关于Android的logcat日志详解的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1145929

相关文章

SpringBoot日志配置SLF4J和Logback的方法实现

《SpringBoot日志配置SLF4J和Logback的方法实现》日志记录是不可或缺的一部分,本文主要介绍了SpringBoot日志配置SLF4J和Logback的方法实现,文中通过示例代码介绍的非... 目录一、前言二、案例一:初识日志三、案例二:使用Lombok输出日志四、案例三:配置Logback一

springboot security快速使用示例详解

《springbootsecurity快速使用示例详解》:本文主要介绍springbootsecurity快速使用示例,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝... 目录创www.chinasem.cn建spring boot项目生成脚手架配置依赖接口示例代码项目结构启用s

Python中随机休眠技术原理与应用详解

《Python中随机休眠技术原理与应用详解》在编程中,让程序暂停执行特定时间是常见需求,当需要引入不确定性时,随机休眠就成为关键技巧,下面我们就来看看Python中随机休眠技术的具体实现与应用吧... 目录引言一、实现原理与基础方法1.1 核心函数解析1.2 基础实现模板1.3 整数版实现二、典型应用场景2

一文详解SpringBoot响应压缩功能的配置与优化

《一文详解SpringBoot响应压缩功能的配置与优化》SpringBoot的响应压缩功能基于智能协商机制,需同时满足很多条件,本文主要为大家详细介绍了SpringBoot响应压缩功能的配置与优化,需... 目录一、核心工作机制1.1 自动协商触发条件1.2 压缩处理流程二、配置方案详解2.1 基础YAML

Python实现无痛修改第三方库源码的方法详解

《Python实现无痛修改第三方库源码的方法详解》很多时候,我们下载的第三方库是不会有需求不满足的情况,但也有极少的情况,第三方库没有兼顾到需求,本文将介绍几个修改源码的操作,大家可以根据需求进行选择... 目录需求不符合模拟示例 1. 修改源文件2. 继承修改3. 猴子补丁4. 追踪局部变量需求不符合很

java中反射(Reflection)机制举例详解

《java中反射(Reflection)机制举例详解》Java中的反射机制是指Java程序在运行期间可以获取到一个对象的全部信息,:本文主要介绍java中反射(Reflection)机制的相关资料... 目录一、什么是反射?二、反射的用途三、获取Class对象四、Class类型的对象使用场景1五、Class

golang 日志log与logrus示例详解

《golang日志log与logrus示例详解》log是Go语言标准库中一个简单的日志库,本文给大家介绍golang日志log与logrus示例详解,感兴趣的朋友一起看看吧... 目录一、Go 标准库 log 详解1. 功能特点2. 常用函数3. 示例代码4. 优势和局限二、第三方库 logrus 详解1.

一文详解如何从零构建Spring Boot Starter并实现整合

《一文详解如何从零构建SpringBootStarter并实现整合》SpringBoot是一个开源的Java基础框架,用于创建独立、生产级的基于Spring框架的应用程序,:本文主要介绍如何从... 目录一、Spring Boot Starter的核心价值二、Starter项目创建全流程2.1 项目初始化(

Spring Boot3虚拟线程的使用步骤详解

《SpringBoot3虚拟线程的使用步骤详解》虚拟线程是Java19中引入的一个新特性,旨在通过简化线程管理来提升应用程序的并发性能,:本文主要介绍SpringBoot3虚拟线程的使用步骤,... 目录问题根源分析解决方案验证验证实验实验1:未启用keep-alive实验2:启用keep-alive扩展建

Java异常架构Exception(异常)详解

《Java异常架构Exception(异常)详解》:本文主要介绍Java异常架构Exception(异常),具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1. Exception 类的概述Exception的分类2. 受检异常(Checked Exception)