安卓logcat命令

本文详细介绍了Android中Logcat的使用方法,包括如何设置过滤条件、输出格式、日志级别及如何将日志输出到文件等高级操作。通过本文,读者可以全面掌握Logcat的使用技巧,提高Android应用的调试效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

常用例子

单个 Tag 过滤

//<tag>[:priority]
//后面优先级不区分大小写,参数区分
adb logcat -s Xposed
//或者
adb logcat  Xposed *:s

多个 Tag 过滤

//<tag>[:priority]
adb logcat ActivityManager:I  MyApp:D *:S

grep 关键字过滤

adb logcat |grep keywords

输出到文件

adb logcat -f /sdcard/log.txt -n2 -r20 log.txt

log.txt
log.txt.1
log.txt.2
后面会会循环输出达到其中3个文件 -r 是大小

logcat

Usage: logcat [options] [filterspecs]
options include:
  -s              Set default filter to silent.
                  Like specifying filterspec '*:s'
  -f <filename>   Log to file. Default to stdout
  -r [<kbytes>]   Rotate log every kbytes. (16 if unspecified). Requires -f
  -n <count>      Sets max number of rotated logs to <count>, default 4
  -v <format>     Sets the log print format, where <format> is one of:

                  brief process tag thread raw time threadtime long

  -c              clear (flush) the entire log and exit
  -d              dump the log and then exit (don't block)
  -t <count>      print only the most recent <count> lines (implies -d)
  -g              get the size of the log's ring buffer and exit
  -b <buffer>     Request alternate ring buffer, 'main', 'system', 'radio'
                  or 'events'. Multiple -b parameters are allowed and the
                  results are interleaved. The default is -b main -b system.
  -B              output the log in binary
filterspecs are a series of
  <tag>[:priority]

where <tag> is a log component tag (or * for all) and priority is:
  V    Verbose
  D    Debug
  I    Info
  W    Warn
  E    Error
  F    Fatal
  S    Silent (supress all output)

'*' means '*:d' and <tag> by itself means <tag>:v

If not specified on the commandline, filterspec is set from ANDROID_LOG_TAGS.
If no filterspec is found, filter defaults to '*:I'

If not specified with -v, format is set from ANDROID_PRINTF_LOG
or defaults to "brief"

参考文档

https://www.cnblogs.com/JianXu/p/5468839.html

Android SDK下, 如何在程序中输出日志 以及如何查看日志. 闲话少说,直接进入正题 在程序中输出日志, 使用 android.util.Log 类. 该类提供了若干静态方法 Log.v(String tag, String msg); Log.d(String tag, String msg); Log.i(String tag, String msg); Log.w(String tag, String msg); Log.e(String tag, String msg); 分别对应 Verbose, Debug, Info, Warning,Error. tag是一个标识,可以是任意字符串,通常可以使用类名+方法名, 主要是用来在查看日志时提供一个筛选条件. 程序运行后 并不会在 ide的控制台内输出任何信息. 如果要后查看日志 请使用 adb logcat 关于adb的更多信息请查看官方网站. 当执行 adb logcat 后会以tail方式实时显示出所有的日志信息. 这时候我们通常需要对信息进行过滤,来显示我们需要的信息, 这时候我们指定的 tag就派上了用场. adb logcat -s MyAndroid:I 这时将只显示tag为MyAndroid,级别为I或级别高于I(Warning,Error)的日志信息. 示例代码如下: Java代码 1. package com.zijun; 2. 3. import android.app.Activity; 4. import android.content.Context; 5. import android.graphics.Canvas; 6. import android.os.Bundle; 7. import android.util.Log; 8. import android.view.MotionEvent; 9. import android.view.View; 10. 11. public class MyAndroid extends Activity { 12. 13. protected static final String ACTIVITY_TAG="MyAndroid"; 14. 15. @Override 16. protected void onCreate(Bundle icicle) { 17. super.onCreate(icicle); 18. setContentView(new MyView(this)); 19. } 20. public class MyView extends View { 21. public MyView(Context c) { 22. super(c); 23. } 24. @Override 25. protected void onDraw(Canvas canvas) { 26. 27. } 28. @Override 29. public boolean onMotionEvent(MotionEvent event) { 30. Log.i(MyAndroid.ACTIVITY_TAG, "============================="); 31. 32. Log.d(MyAndroid.ACTIVITY_TAG, "Haha , this is a DEBUG of MyAndroid. "); 33. Log.i(MyAndroid.ACTIVITY_TAG, "Haha , this is a INFO of MyAndroid. "); 34. Log.w(MyAndroid.ACTIVITY_TAG, "Haha , this is a WARNING of MyAndroid. "); 35. 36. return true; 37. } 38.
### Logcat 命令使用教程 Logcat 是 Android 提供的强大日志系统工具,用于帮助开发者实时查看和分析设备或模拟器上的日志信息。以下是关于 logcat 的基本概念、常用命令以及如何有效过滤日志的具体说明。 #### 一、Logcat 的基本概念 Logcat 是一种日志记录机制,它允许开发者捕获来自 Android 设备的日志消息并将其显示出来。这些日志可以分为不同的优先级级别,分别是 Verbose (V)、Debug (D)、Info (I)、Warn (W) 和 Error (E)[^1]。每种级别的日志适用于不同类型的事件报告。 #### 二、常用的 Logcat 命令 以下是一些常见的 `adb logcat` 命令及其功能: - **基础命令** 运行以下命令可以在终端中启动 logcat 并持续监控日志输出: ```bash adb logcat ``` - **保存日志到文件** 将日志输出重定向至指定路径下的文件以便后续查阅: ```bash adb logcat -f /sdcard/log.txt ``` 此操作会将所有日志写入 `/sdcard/log.txt` 文件中[^2]。 - **清除当前日志缓冲区** 清除之前存储的所有日志数据以获得干净的工作环境: ```bash adb logcat -c ``` #### 三、日志过滤技术 为了专注于特定部分的日志而不受其他无关信息干扰,可采用多种方式来筛选所需内容。 - **按标签过滤** 如果只关心某个组件产生的日志,则可以通过设置相应的 TAG 来实现这一点。例如仅展示标记为 "ActivityManager" 的 Debug 日志: ```bash adb logcat ActivityManager:D *:S ``` - **组合多个条件** 可同时应用多条规则进一步缩小范围。比如既关注活动管理又留意键盘更新状态的变化情况时可用如下指令: ```bash adb logcat *:S ActivityManager:D KeyguardUpdateMonitor:D ``` - **基于关键字查找** 利用 grep 工具配合管道符完成复杂查询需求。假设想找出包含字符串 “error” 的所有错误等级以上的记录则执行下面语句即可达成目的: ```bash adb logcat | grep error ``` 另外,在高级场景下还可以借助正则表达式增强匹配精度;或者利用 level 参数限定最低严重程度阈值如 ERROR 或 WARNING 等[^3][^4]。 --- ### 示例代码片段 这里给出一段简单的 Python 脚本来演示如何自动化处理 ADB 输出结果: ```python import subprocess def get_logcat_output(filter_spec="*"): command = f"adb logcat {filter_spec}" process = subprocess.Popen(command.split(), stdout=subprocess.PIPE) while True: output_line = process.stdout.readline().decode('utf-8').strip() if not output_line and process.poll() is not None: break yield output_line for line in get_logcat_output("ActivityManager:I"): print(line) ``` 此脚本定义了一个函数用来获取经过简单过滤后的连续日志流,并逐行打印它们直到进程结束为止。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

骇客之技术

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值