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

相关文章

一文深入详解Python的secrets模块

《一文深入详解Python的secrets模块》在构建涉及用户身份认证、权限管理、加密通信等系统时,开发者最不能忽视的一个问题就是“安全性”,Python在3.6版本中引入了专门面向安全用途的secr... 目录引言一、背景与动机:为什么需要 secrets 模块?二、secrets 模块的核心功能1. 基

一文详解MySQL如何设置自动备份任务

《一文详解MySQL如何设置自动备份任务》设置自动备份任务可以确保你的数据库定期备份,防止数据丢失,下面我们就来详细介绍一下如何使用Bash脚本和Cron任务在Linux系统上设置MySQL数据库的自... 目录1. 编写备份脚本1.1 创建并编辑备份脚本1.2 给予脚本执行权限2. 设置 Cron 任务2

一文详解如何在idea中快速搭建一个Spring Boot项目

《一文详解如何在idea中快速搭建一个SpringBoot项目》IntelliJIDEA作为Java开发者的‌首选IDE‌,深度集成SpringBoot支持,可一键生成项目骨架、智能配置依赖,这篇文... 目录前言1、创建项目名称2、勾选需要的依赖3、在setting中检查maven4、编写数据源5、开启热

Python常用命令提示符使用方法详解

《Python常用命令提示符使用方法详解》在学习python的过程中,我们需要用到命令提示符(CMD)进行环境的配置,:本文主要介绍Python常用命令提示符使用方法的相关资料,文中通过代码介绍的... 目录一、python环境基础命令【Windows】1、检查Python是否安装2、 查看Python的安

HTML5 搜索框Search Box详解

《HTML5搜索框SearchBox详解》HTML5的搜索框是一个强大的工具,能够有效提升用户体验,通过结合自动补全功能和适当的样式,可以创建出既美观又实用的搜索界面,这篇文章给大家介绍HTML5... html5 搜索框(Search Box)详解搜索框是一个用于输入查询内容的控件,通常用于网站或应用程

SpringBoot项目配置logback-spring.xml屏蔽特定路径的日志

《SpringBoot项目配置logback-spring.xml屏蔽特定路径的日志》在SpringBoot项目中,使用logback-spring.xml配置屏蔽特定路径的日志有两种常用方式,文中的... 目录方案一:基础配置(直接关闭目标路径日志)方案二:结合 Spring Profile 按环境屏蔽关

Python中使用uv创建环境及原理举例详解

《Python中使用uv创建环境及原理举例详解》uv是Astral团队开发的高性能Python工具,整合包管理、虚拟环境、Python版本控制等功能,:本文主要介绍Python中使用uv创建环境及... 目录一、uv工具简介核心特点:二、安装uv1. 通过pip安装2. 通过脚本安装验证安装:配置镜像源(可

C++ 函数 strftime 和时间格式示例详解

《C++函数strftime和时间格式示例详解》strftime是C/C++标准库中用于格式化日期和时间的函数,定义在ctime头文件中,它将tm结构体中的时间信息转换为指定格式的字符串,是处理... 目录C++ 函数 strftipythonme 详解一、函数原型二、功能描述三、格式字符串说明四、返回值五

LiteFlow轻量级工作流引擎使用示例详解

《LiteFlow轻量级工作流引擎使用示例详解》:本文主要介绍LiteFlow是一个灵活、简洁且轻量的工作流引擎,适合用于中小型项目和微服务架构中的流程编排,本文给大家介绍LiteFlow轻量级工... 目录1. LiteFlow 主要特点2. 工作流定义方式3. LiteFlow 流程示例4. LiteF

CSS3中的字体及相关属性详解

《CSS3中的字体及相关属性详解》:本文主要介绍了CSS3中的字体及相关属性,详细内容请阅读本文,希望能对你有所帮助... 字体网页字体的三个来源:用户机器上安装的字体,放心使用。保存在第三方网站上的字体,例如Typekit和Google,可以link标签链接到你的页面上。保存在你自己Web服务器上的字