Android adb命令logcat日志分析

这篇具有很好参考价值的文章主要介绍了Android adb命令logcat日志分析。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。


一、简介

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


二、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 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"

2.1 adb logcat 命令格式

adb logcat [<option>] ... [<filter-spec>] ...
adb logcat [选项...] [过滤项...],

2.2 adb logcat 命令参数

选项 描述 举例
-s 输出指定 tag 的日志,相当于过滤器表达式 *:S logcat -s tag
-f <file> 设置logcat 内容保存的位置,默认是stdout logcat -f sdcard/log.txt
-r <kbytes> 每输出 时轮替日志文件,默认是16 必须配合 -f logcat -f sdcard/log.txt -r 1
-n <count> 设置日志输出的最大数目, 需要 -r 参数
-v <format> 设置日志的输出格式,注意只能设置一项;详见下文 格式化输出 logcat -v threadtime
-D 输出各个日志缓冲区之间的分隔线 logcat -D …
-c 清除(清空)所选的缓冲区并退出,默认清除 mainsystemcrash logcat -c;logcat -b all -c
-d 将日志转储到屏幕并退出 logcat -d > log.txt
-e <expr> 输出正则匹配的日志消息 logcat -e [匹配数据] -m 5
-m <count> 输出 行后退出 logcat -m 5
-t <count> 仅输出最新的行数,此选项包括 -d 功能 logcat -t 5
-t '<time>' 输出自指定时间以来的最新行,此选项包括 -d 功能 logcat -t ‘01-26 20:52:41.820’
-g 获取指定日志缓冲区的大小并退出 logcat -g
-G 设置日志环形缓冲区的大小,可以在结尾处添加 KM logcat -G 2M
-b 加载可供查看的日志缓冲区,更多可见下文 日志缓冲区 logcat -b system
-B 以二进制文件形式输出日志
-S 在输出中包含统计信息,以识别和定位日志垃圾信息发送者
--pid=<pid> 仅输出来自给定 PID 的日志 logcat --pid=4321

2.3 adb 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

2.4 adb logcat 格式化输出

使用 -v 命令来修改 log 的输出格式,以显示特定的元数据字段:

格式 描述
brief 显示优先级、标记以及发出消息的进程的 PID
long 显示所有元数据字段,并使用空白行分隔消息
process 仅显示 PID
raw 显示不包含其他元数据字段的原始日志消息
tag 仅显示优先级和标记
thread 旧版格式,显示优先级、PID 以及发出消息的线程的 TID
threadtime (默认值)显示日期、调用时间、优先级、标记、PID 以及发出消息的线程的 TID
time 显示日期、调用时间、优先级、标记以及发出消息的进程的 PID
color 使用不同的颜色来显示每个优先级
descriptive 显示日志缓冲区事件说明。此修饰符仅影响事件日志缓冲区消息,不会对其他非二进制文件缓冲区产生任何影响
epoch 显示自 1970 年 1 月 1 日以来的时间(以秒为单位)
uid 如果访问控制允许,则显示 UID 或记录的进程的 Android ID
usec 显示精确到微秒的时间
UTC 显示 UTC 时间
year 将年份添加到显示的时间
zone 将本地时区添加到显示的时间

对于 -v 选项,如果需要查看线程区别使用 logcat -v threadtime 就可以了,其他日志基本也是少用的。

下面列举几种输出格式进行说明。


2.4.1 logcat -v brief

格式:

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

示例:

D/TAG( 1785): Disconnected process message: 10, size: 0

2.4.2 logcat -v long

格式:

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

示例:

[ 08-28 22:39:39.974  1785: 1832 D/TAG ] Disconnected process message: 10, size: 0

2.4.3 logcat -v process

格式:

<priority>(<pid>) <message>

示例:

D( 1785) Disconnected process message: 10, size: 0 

2.4.4 logcat -v tag

格式:

<priority>/<tag>: <message>

示例:

D/TAG: Disconnected process message: 10, size: 0

2.4.5 logcat -v raw

格式:

<message>

示例:

Disconnected process message: 10, size: 0

2.4.6 logcat -v time

格式:

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

示例:

08-28 22:39:39.974 D/TAG( 1785): Disconnected process message: 10, size: 0

2.4.7 logcat -v threadtime

格式:

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

示例:

08-28 22:39:39.974  1785 1832 D TAG: Disconnected process message: 10, size: 0

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


三、adb logcat 示例

多数情况下,logcat命令参数都是组合使用。


3.1 adb logcat -b all -v threadtime --pid=4321 > /Users/xxx/logcat.log

将进程ID为4321的应用,所有缓冲区日志,按照 threadtime 格式输出到 logcat.log 日志文件中。

adb logcat -b all -v threadtime --pid=4321 > /Users/xxx/logcat.log

3.2 adb logcat -b all -c

将所有缓冲区日志清空。

adb logcat -b all -c

3.3 adb logcat -b all -d -v threadtime --pid=4321 -t 10

将进程ID为4321的应用,所有缓冲区日志,按照 threadtime 格式输出最新的10行日志(最多10行)。

adb logcat -b all -d -v threadtime --pid=4321 -t 10

四、Android 代码操作logcat日志

4.1 Android代码读取logcat日志

private void getLogcat(){
	String[] comand = new String[]{"logcat", "-b", "all", "-d", "-v", "threadtime", "--pid=4321", "-t", "10"};
    try{
        Process exec = Runtime.getRuntime().exec(comand);
        BufferedReader reader = new BufferedReader(new InputStreamReader(exec.getInputStream()));
        StringBuilder sb = new StringBuilder();
        String line;
        int i = 0;
        while ((line = reader.readLine()) != null) {
            sb.append(line).append("\r\n");
        }
        Log.i("TAG", "logcat content = " + sb.toString());
    }catch (Exception ex){
        Log.e("TAG", "Exception = " + ex.toString());
    }
}

4.2 Android代码实现将logcat日志输出到文件

public class LogcatProcesser {
	private Process logcatToFileProcess;

	/**
     * 获取日志输出文件
     */
	private String getLogcatFile(){
        String path = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "cwlogcat";
        File file = new File(path);
        if(!file.exists()){
            file.mkdirs();
        }
        return new File(file.getAbsolutePath() + File.separator + "cw_logcat.log").getAbsolutePath();
    }
    
	/**
     * 开启日志输出到文件
     */
	private void startLogcatToFile(){
        int pid = android.os.Process.myPid();
        String path = getLogcatFile();
        try{
            String[] cmds = new String[]{"logcat","-b", "all", "-v", "threadtime", "--pid=" + pid, "-f", path};
            if(logcatToFileProcess == null) {
            	// 持续将日志输出到日志文件中
                ProcessBuilder processBuilder = new ProcessBuilder(cmds);
                // 启动进程并重定向输出
                logcatToFileProcess = processBuilder.start();
                // 读取进程的错误流,以防止阻塞
                readStream(logcatToFileProcess.getErrorStream());
            }
        }catch (Exception ex){
            Log.e("TAG", "Exception = " + ex.toString());
        }
    }
    
    /**
     * 停止日志输出到文件
     */
	private void stopLogcatToFile(){
        try{
            if(logcatToFileProcess == null){
                return;
            }

            logcatToFileProcess.destroy();
            logcatToFileProcess = null;
        }catch (Exception ex){
            Log.e("TAG", "Exception = " + ex.toString());
        }
    }

	/**
	 * 读取进程的错误流
	 */
	private void readStream(final java.io.InputStream is) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    byte[] buffer = new byte[1024];
                    while (is.read(buffer) != -1) {
                        // 读取并忽略输入流中的数据
                    }
                    is.close();
                } catch (IOException e) {
                    Log.e("TAG", "Exception = " + ex.toString());
                }
            }
        }).start();
    }
}



文章来源地址https://www.toymoban.com/news/detail-848776.html

到了这里,关于Android adb命令logcat日志分析的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 使用ADB命令查询Android设备的安卓版本信息

    确保你已经安装了ADB并将其添加到系统的路径中。 连接你的Android设备到计算机,并确保启用了开发者选项和USB调试模式。你可以在设备的设置中找到这些选项。 打开终端(命令提示符或终端窗口)。 运行以下ADB命令来获取Android设备的安卓版本信息: 如果你希望获得更详细

    2024年02月08日
    浏览(35)
  • Ubuntu 18.04.6 Android Studio Giraffe adb logcat 无法使用

    在 Ubuntu 18.04.6 上 在链接上设备以后,发现可以用 Android Studio 安装应用 但无法用 Android Studio 看 logcat 手动从命令行停止,启动 adb 会报错如下: Android Studio 下面 logcat 标签页,设备选择框显示 \\\"no connected devices\\\",但却可以正常安装 app,就是没 log。 我这里的错误原因是因为之前

    2024年02月13日
    浏览(31)
  • Android JNI打印logcat日志

    在 JNI 中打印日志可以使用 __android_log_print 函数来实现。该函数是 Android NDK 提供的一个用于在本地代码中输出日志消息到 logcat 的方法。 要在 JNI 中打印日志,请按照以下步骤进行操作: 在你的 JNI C/C++ 代码中包含 android/log.h 头文件: 使用 __android_log_print 函数来打印日志。它

    2024年02月09日
    浏览(35)
  • 【Android】logcat日志敏感信息泄露

    之前会遇到一些应用logcat打印敏感信息,包括但不限于账号密码,cookie凭证,或一些敏感的secretkey之类的,下面客观的记录下起危害性。  1. logcat logcat是Android系统提供的一种记录日志的工具。它可以帮助开发人员诊断应用程序中的问题,例如崩溃、内存泄漏和性能问题。l

    2024年02月07日
    浏览(38)
  • 新版Android Studio Logcat 筛选日志

    下载了新版的Android Studio,android-studio-2022.3.1.21-mac_arm,记录一下新版本AS的logcat过滤日志条件 1.1 过滤当前包名的日志 1.2 过滤其他包名日志 在控制台输入 就可以筛选所有info级别的日志 3.1 筛选单个tag 比如想筛选MainActivity的所有日志 那么在控制台可以看到所有MainActivity的日志

    2024年02月22日
    浏览(38)
  • Android11 默认开机后台记录logcat日志

    软件平台:Android11 硬件平台:QCS6125     需求:android启动后,默认后台记录logcat日志,并且设置每个文件大小为2M,最多记录30个文件,并且放置的data目录需要开放给app侧读取权限。 改动如下: device目录添加app侧读取的selinux权限: system目录相关添加: Mark一下,编译验证调

    2024年02月15日
    浏览(32)
  • Android --- logcat 打印日志、设置缓存大小、过滤等

    一、示例: Android log 通过系统属性 persist.log.tag 和 persist.log.tag.ABC  决定相关log的指定等级及以上才可以写入,ABC是指某个tag,TAG长度不能太长。 二、设置系统属性: 1.设置log日志缓存大小 2.使能所有log tag 输出,设置所有log V等级及以上才能输出 3.禁止所有log tag 的输出,设置

    2023年04月09日
    浏览(32)
  • Android Studio中Logcat日志不显示的解决办法

    Android Studio中Logcat日志不显示的解决办法 近期,一些开发者反映在使用Android Studio进行应用程序开发时,Logcat日志突然不显示的问题。这种情况可能会给调试和排错带来困扰。在本文中,我将为你提供一些解决该问题的方法。 以下是可能导致Logcat日志不显示的常见原因以及相

    2024年02月08日
    浏览(35)
  • Android Studio调试的时候Logcat不显示日志了

    使用Log输出日志的时候,Logcat窗口并没有显示日志。 去除所有的过滤条件之后,Logcat窗口仍然没有一条消息。 关闭Android Studio,重启Android Studio即可。 安卓获取当前设备的局域网IP地址 安卓监听端口接收消息 如果这个方法并不奏效,还有其他解决方案可以尝试。 日志级别决

    2024年02月12日
    浏览(34)
  • android studio logcat 无日志 No connect devices

    去sdk tools中找到 google use driver,下载,然后重启编译器 连接不上夜神模拟器可以去夜神对应的bin目录下,在目录框中输入cmd回车 输入nox_adb.exe connect 127.0.0.1:62001 然后就能找到设备了,如果还找不到,可能需要去更换adb版本; 2023年5月11日11:43:18 补充 用上面方法后续还是会发

    2024年02月13日
    浏览(31)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包