一、logcat对日志过滤

1.# logcat --help

# logcat --help
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. ( if unspecified). Requires -f
-n <count> Sets max number of rotated logs to <count>, default
-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)
-t '<time>' print most recent lines since specified time (implies -d)
-T <count> print only the most recent <count> lines (does not imply -d)
-T '<time>' print most recent lines since specified time (not imply -d)
count is pure numerical, time is 'MM-DD hh:mm:ss.mmm'
-g get the size of the log's ring buffer and exit
-b <buffer> Request alternate ring buffer, 'main', 'system', 'radio',
'events', 'crash' or 'all'. Multiple -b parameters are
allowed and results are interleaved. The default is
-b main -b system -b crash.
-B output the log in binary.
-S output statistics.
-G <size> set size of log ring buffer, may suffix with K or M.
-p print prune white and ~black list. Service is specified as
UID, UID/PID or /PID. Weighed for quicker pruning if prefix
with ~, otherwise weighed for longevity if unadorned. All
other pruning activity is oldest first. Special case ~!
represents an automatic quicker pruning for the noisiest
UID as determined by the current statistics.
-P '<list> ...' set prune white and ~black list, using same format as
printed above. Must be quoted. 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"

1. logcat命令位置:/system/bin/logcat

2.logcat的常用用法

# logcat *:W 过滤日志则会将该级别及以上的日志输出

# logcat ActivityManager:I MyApp:D *:S 表示输出tag=“ActivityManager”的Info以上级别日志,输出tag=“MyApp”的Debug以上级别日志,及其它tag的Silent级别日志(即屏蔽其它 tag 日志)。

# logcat -d logcat应该是动态打印的,加-d后打印完log后退出,不阻塞
# logcat -t 20 只打印最近的20行log,-T也是类似的功能,但是打印完后不退出。
# logcat -f a.txt -f保存到文件中,不使用重定向符号的
# logcat | grep -i myapp 忽略大小写。
# logcat | grep --color=auto -i priv-app 匹配到的“priv-app”会显示出颜色
# logcat | grep --color=auto 1979 只打印出进程PID为1979的log
# logcat -c && logcat
# logcat -v time 打印时加上时间
# logcat -g 打印各个环形缓冲区的大小后退出
# logcat -S 打印出统计信息
# logcat | grep -E "Theft|u-blox" grep -E 正则表达式,可以一次性检索多个关键字
# logcat <tag1>:<priority1> <tag2>:<priority2> 过滤低优先级的日志

# logcat *:E   只查看错误打印

3.有 3 种方式使用 logcat 打印日志:
可以通过 IDE(比如 Android Studio)提供的 logcat 窗口查看打印的日志
可以通过 adb 命令使用 logcat 在本地计算机进行日志查看
通过 adb 命令进入模拟器或者所连设备的 shell 命令窗口,进行 logcat 的操作和使用

4.设置输出日志等级
通过设置优先级,过滤掉低优先级的日志,使用方式如: adb logcat <tag1>:<priority1> <tag2>:<priority2>
示例: logcat PowerManagerService:I PackageManager:I 表示logcat时不打印PowerManagerService和PackageManager的优先级低于Info的log

5.logcat选择打印的缓冲区:
# logcat -b <buffer> 可以指定要打印的环缓冲区,“main”,“system”,“radio”或“events”。 允许多个-b连用,这样打印的结果是交错的,默认为: -b main -b system -b crash。
radio - 查看缓冲区的相关的信息.
events - 查看和事件相关的的缓冲区.
main - 查看主要的日志缓冲区.
system - 与系统相关的日志信息.

6. logcat默认的几种输出格式为

格式:logcat -v threadtime

(1)brief格式为:<priority>/<tag>(<pid>): <message>  eg: D/HeadsetStateMachine( 1785): Disconnected process message: 10, size: 0

(2)process格式为:<priority>(<pid>) <message>  eg: D( 1785) Disconnected process message: 10, size: 0 (HeadsetStateMachine)

(3)tag格式为:<priority>/<tag>: <message>  eg: D/HeadsetStateMachine: Disconnected process message: 10, size: 0

(4)raw格式为:<message>   eg: Disconnected process message: 10, size: 0

(5)time格式为:<datetime> <priority>/<tag>(<pid>): <message>  eg: 08-28 22:39:39.974 D/HeadsetStateMachine( 1785): Disconnected process message: 10, size: 0

(6)threadtime格式为:<datetime> <pid> <tid> <priority> <tag>: <message>  eg: 08-28 22:39:39.974 1785 1832 D HeadsetStateMachine: Disconnected process message: 10, size: 0

(7)long格式为:[ <datetime> <pid>:<tid> <priority>/<tag> ] <message>  eg: [ 08-28 22:39:39.974 1785: 1832 D/HeadsetStateMachine ] Disconnected process  message: 10, size: 0

二、日志打印函数

1.程序中使用ALOGx(),SLOGx(),RLOGx()来分别打印app,system,radio的log,

x表示下面6种打印级别:
V Verbose
D Debug
I Info
W Warn
E Error
F Fatal

比如:
#define LOG_TAG "LedHal"
ALOGI("hal_led_ioctl called: which=%d, status=%d", which, status);
打印如下:

I/LedHal ( 2284): hal_led_ioctl called: which=0, status=0 //打印等级/Tag (PID): message

三、日志统计

1.# logcat -b all -S 可以统计出各个log buffer中最健谈的log的进程、UID、TAG

# logcat -b all -S
size/num main radio events system crash stats performance security kernel Total
Total / / / / / / / / / /
Now / / / / / / /
Logspan ::38.987(3.9%) ::44.731(24.2%) ::41.166(96.9%) ::44.791(%) :03.21 ::35.716 ::48.058
Overhead
update time=, report time=, buffer rate = Byte/sec, log rate = sec-
buffer chatty rate = Byte/sec, log chatty rate = sec- 最健谈的main log:
Chattiest UIDs in main log buffer: Size +/- Pruned
UID PACKAGE BYTES NUM
com.android.systemui -%
system -%
PID/UID COMMAND LINE " "
/ system_server
/ com.coloros.persist.system 最健谈的main log:
Chattiest UIDs in radio log buffer: Size +/- Pruned
UID PACKAGE BYTES NUM
radio -3.6%
system .2X 最健谈的event log:
Chattiest UIDs in events log buffer: Size +/- Pruned
UID PACKAGE BYTES NUM
system +4.3%
PID/UID COMMAND LINE " "
/ system_server 最健谈的system log:
Chattiest UIDs in system log buffer: Size +/- Pruned
UID PACKAGE BYTES NUM
system -%
PID/UID COMMAND LINE " "
/ system_server
/ com.coloros.persist.system 最健谈的crash log:
Chattiest UIDs in crash log buffer: Size
UID PACKAGE BYTES
root 最健谈的performance log:
Chattiest UIDs in performance log buffer: Size
UID PACKAGE BYTES
system
PID/UID COMMAND LINE "
/ system_server 最健谈的PID:
Chattiest PIDs: Size Pruned
PID/UID COMMAND LINE BYTES NUM
/ com.android.phone
/ system_server 最健谈的TID:
Chattiest TIDs: Size Pruned
TID/UID COMM BYTES NUM
/ com.android.phone
/ android.bg 最健谈的events log中的TAG:
Chattiest events log buffer TAGs: Size Prune
TAG/UID TAGNAME BYTES NUM
auditd
/ am_pss 最健谈的TAG:
Chattiest TAGs: Size
TID/PID/UID LOG_TAG NAME BYTES
/ SST
auditd

Android中logcat和日志打印的更多相关文章

  1. Android中对Log日志文件的分析[转]

    一,Bug出现了, 需要“干掉”它 bug一听挺吓人的,但是只要你懂了,android里的bug是很好解决的,因为android里提供了LOG机制,具体的底层代码,以后在来分析,只要你会看bug, a ...

  2. 02_JNI中Java代码调用C代码,Android中使用log库打印日志,javah命令的使用,Android.mk文件的编写,交叉编译

     1  编写以下案例(下面的三个按钮都调用了底层的C语言): 项目案例的代码结构如下: 2 编写DataProvider的代码: package com.example.ndkpassdata; ...

  3. android adb logcat获取日志文件

    一般情况,可以直接在电脑终端上输入logcat来查看手机运行日志,但是测试工作者为了抓取日志文件来给开发人员,需要把日志导出到特定文件中.如下文. 以小米1s手机为例 步骤1:打开第一个终端窗口 ad ...

  4. android中Logcat的深层理解

    Android的开发也能够归类为嵌入式设备的开发.即便不是嵌入式开发,依旧要注意对内存和处理的使用.养成一个好的习惯对自己的帮助是非常大的. 在Log的源代码中能够看到这种凝视: The order ...

  5. 【转载】理解Android中垃圾回收日志信息

    本文转自:http://droidyue.com/blog/2014/11/08/understanding-garbage-collection-output-messages-in-android ...

  6. Android中垃圾回收日志信息

    原因 GC_CONCURRENTfreed 178K, 41% free 3673K/6151K, external 0K/0K, paused 2ms+2msGC_EXPLICITfreed 6K, ...

  7. 在android中使用logback-android日志框架配置 slf4j + logback

    为什么使用 slf4j + logback logbak定位于log4j的替代者,logback同样支持slf4j,方便被替换.在Android平台上,我在使用log4中遇到tag混乱的问题.相比lo ...

  8. Android adb logcat输出日志显示不全解决方案

    在终端中使用adb logcat打印服务器json数据,如果返回数据过大超过4000字节(4K)即会截断不显示 原因:logcat在对于message的内存分配大概是4k左右.所以超过的内容都直接被丢 ...

  9. android中Logcat的TAG过滤

    如果代码中有这样的log: Log.e("Foo", "error in foo"); Log.d("Foo", "debug i ...

随机推荐

  1. (转)sublime text3 3176激活

    更改hosts:sudo vim /private/etc/hosts 127.0.0.1 www.sublimetext.com 127.0.0.1 license.sublimehq.com 激活 ...

  2. 使用nginx+dnsmasq解决同IP不同端口Session冲突问题

    由于一台服务器上需要部署多个项目,而我们的WEB项目因为用到框架都是一样的,导致同时运行,session相互冲突,这个登录后,那个就得重新登录,造成了使用不方便,原因是IP相同认为是同一个域,接收了B ...

  3. Jzzhu and Numbers CodeForces - 449D (高维前缀和,容斥)

    大意: 给定集合a, 求a的按位与和等于0的非空子集数. 首先由容斥可以得到 $ans = \sum \limits_{0\le x <2^{20}} (-1)^{\alpha} f_x$, 其 ...

  4. in_array的效率

    in_array函数是个糟糕的选择.应该尽量用isset函数或array_key_exists函数来替代 .in_array函数的复杂度是O(n),而isset函数的复杂度是O(1) isset函数是 ...

  5. 『cs231n』注意力模型

    RNN实现文本标注: 弊端是图像信息只在初始化时有用到 Soft Attention模型: 每一层具有三个输入:隐藏状态 + 注意力特征向量 + 词向量 每一层具有两个输出:新的位置分布(指示下一次‘ ...

  6. 『OpenCV3』处理视频&摄像头

    在opencv中,摄像头和视频文件并没有很大不同,都是一个可以read的数据源,使用cv2.VideoCapture(path).read()可以获取(flag,当前帧),对于每一帧,使用图片处理函数 ...

  7. python-day6---while循环

    # while 条件:# 循环体的代码1# 循环体的代码2# 循环体的代码3# count=0# while count < 10:# print(count)# count+=1 # whil ...

  8. Oracle 使用GSON库解析复杂json串

    在前文中讲到了如何使用JSON标准库解析json串,参考: Oracle解析复杂json的方法(转) 现补充一篇使用GSON库在Oracle中解析复杂json的方法. GSON串的使用教程参考官方文档 ...

  9. maven 构建war包时排除web.xml

    在使用maven构建项目的war包时,有时并不需要src/webapp/WEB-INF/下的一些文件. 这时可以通过maven-war-plugin创建配置来排除这些文件.下面贴出我平时使用的pom. ...

  10. JS实现点击按钮,下载文件

    PS:本文说的,并非如何用js创建流.创建文件.实现下载功能. 而是说的:你已知一个下载文件的后端接口,前端如何请求该接口,实现点击按钮.下载文件到本地.(可以是zip啦.excel啦都是一样) 有两 ...