App测试-logcat

1、解释adb logcat的帮助信息

在命令行中输入 adb logcat --help 命令, 就可以显示该命令的帮助信息;

C:\Program Files (x86)\Nox\bin>adb 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. (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"

adb logcat 命令格式:

adb logcat [选项] [过滤器],其中 选项 和 过滤项 在 中括号 [] 中, 说明这是可选的;

1) 选项解析

  • 选项解析 :

-- "-s"选项 : 设置输出日志的标签, 只显示该标签的日志;

--"-f"选项 : 将日志输出到文件, 默认输出到标准输出流中, -f 参数执行不成功;

--"-r"选项 : 按照每千字节输出日志, 需要 -f 参数, 不过这个命令没有执行成功;

--"-n"选项 : 设置日志输出的最大数目, 需要 -r 参数, 这个执行 感觉 跟 adb logcat 效果一样;

--"-v"选项 : 设置日志的输出格式, 注意只能设置一项;

--"-c"选项 : 清空所有的日志缓存信息;

--"-d"选项 : 将缓存的日志输出到屏幕上, 并且不会阻塞;

--"-t"选项 : 输出最近的几行日志, 输出完退出, 不阻塞;

--"-g"选项 : 查看日志缓冲区信息;

--"-b"选项 : 加载一个日志缓冲区, 默认是 main, 下面详解;

--"-B"选项 : 以二进制形式输出日志;

  • 输出指定标签内容 :
    -- "-s"选项 :
    设置默认的过滤器, 如 我们想要输出 "System.out" 标签的信息, 就可以使用
adb logcat -s System.out;
octopus@octopus:~$ adb logcat -s System.out  
--------- beginning of /dev/log/system  
--------- beginning of /dev/log/main  
I/System.out(22930): GSM -91  
I/System.out(22930): SignalStrength issssssssss : -91  
I/System.out(22930): GSM -91  
I/System.out(22930): SignalStrength issssssssss : -91  
I/System.out(22930): Supervisor Thread  
I/System.out(22930): Got run mode  
  • 输出日志信息到文件 :
    -- "-f"选项 :
    该选向后面跟着输入日志的文件, 使用adb logcat -f /sdcard/log.txt 命令, 注意这个log文件是输出到手机上,需要指定合适的路径。
adb logcat -f /sdcard/log.txt   

这个参数对对不能一直用电脑连着手机收集日志的场景非常有用,其实android shell下也有一个相同参数的logcat命令。使用如下命令可以执行后断开PC和手机持续收集LOG。

shell@pc$ adb shell  
shell@android$ logcat -f /sdcard/log.txt &   
#这里的&符号表示后台执行,别少了。  
shell@android$ exit  

注:

(1)以上shell@pc$ 指在pc的shell终端执行后边的命令, shell@android$ 表示在手机shell中执行后边的命令l

(2)一定注意合适的时候需要停止掉以上命令,否则再次使用相同命令的时候,就会有两个logcat写同一个文件了

      停止方法:  adb shell kill -9 <logcat_pid>         

       其中logcat_pid 通过 如下命令获取

       adb shell ps | grep logcat          # linux 平台

       adb shell ps | findstr "logcat"    #Windows平台

-- ">"输出 :
">" 后面跟着要输出的日志文件, 可以将 logcat 日志输出到文件中, 使用adb logcat > log 命令, 使用more log 命令查看日志信息;

octopus@octopus:~$ adb logcat > log  
^C  
octopus@octopus:~$ more log  
--------- beginning of /dev/log/system  
V/ActivityManager(  500): We have pending thumbnails: null  
V/ActivityManager(  500): getTasks: max=1, flags=0, receiver=null  
V/ActivityManager(  500): com.android.settings/.Settings: task=TaskRecord{42392278 
#448 A com.android.settings U 0}  
V/ActivityManager(  500): We have pending thumbnails: null  

-- " -d -f <log>" 组合命令:
可以将日志保存到手机上的指定位置,对不能一直用电脑连着手机收集日志的场景非常有用。

adb logcat -d -v /sdcard/mylog.txt  
  • 指定 logcat 的日志输出格式 :
    -- "-v"选项 :
    使用adb logcat -v time 命令, 可以啥看日志的输出时间; 使用adb logcat -v threadtime 命令, 可以啥看日志的输出时间和线程信息;

-- "brief"格式 :
这是默认的日志格式" 优先级 / 标签 (进程ID) : 日志信息 ", 使用adb logcat -v prief 命令;

octopus@octopus:~$ adb logcat -v brief  
--------- beginning of /dev/log/system  
D/PowerManagerService(  500): handleSandman: canDream=true, mWakefulness=Awake  
D/PowerManagerService(  500): releaseWakeLockInternal: lock=1101267696, flags=0x0  

-- "process"格式 :
" 优先级 (进程ID) : 日志信息 ", 使用adb logcat -v process 命令;

octopus@octopus:~$ adb logcat -v process  
--------- beginning of /dev/log/system  
D(  500) MobileDataStateReceiver received: ACTION_ANY_DATA_CONNECTION_STATE_CHANGED_MOBILE [wap]  (MobileDataStateTracker)  
V(  500) Broadcast: Intent { act=android.intent.action.ANY_DATA_STATE_MOBILE flg=0x10 (has extras) } ordered=true userid=0  (ActivityManager)  
D(  500) wap: Intent from SIM 0, current SIM 0, current DataState DISCONNECTED  (MobileDataStateTracker)  
D(  500) wap: wap setting isAvailable to false  (MobileDataStateTracker)  
D(  500) wap: Received state=DISCONNECTED, old=DISCONNECTED, reason=dataDetached  (MobileDataStateTracker)  
D(  500) BDC-Calling finishReceiver: IIntentReceiver=41c46ba0  (ActivityThread)  

-- "tag"格式 :
" 优先级 / 标签 : 日志信息", 使用adb logcat -v tag 命令;

octopus@octopus:~$ adb logcat -v tag  
--------- beginning of /dev/log/system  
I/PowerManagerService: setBrightness mButtonLight 0.  
D/PowerManagerService: updateScreenStateLocked: mDisplayReady=true, newScreenState=2, mWakefulness=1, mWakeLockSummary=0x1, mUserActivitySummary=0x1, mBootCompleted=true  
D/PowerManagerService: handleSandman: canDream=true, mWakefulness=Awake  

-- "thread"格式 :
" 优先级 ( 进程ID : 线程ID) 标签 : 日志内容 ", 使用adb logcat -v tag 命令;

octopus@octopus:~$ adb logcat -v thread  
--------- beginning of /dev/log/system  
V(  500: 2141) getTasks: max=1, flags=0, receiver=null  
V(  500: 2141) com.lewa.launcher/.Launcher: task=TaskRecord{41dccc20 #425 A com.lewa.launcher U 0}  
V(  500: 2141) We have pending thumbnails: null  
V(  500: 2140) getTasks: max=1, flags=0, receiver=null  

-- "raw"格式 :
只输出日志信息, 不附加任何其他 信息, 如 优先级 标签等, 使用adb logcat -v raw 命令;

octopus@octopus:~$ adb logcat -v raw  
--------- beginning of /dev/log/system  
notifications are enabled for com.kindroid.security  
Assigned score=0 to Notification(pri=0 contentView=com.kindroid.security/0x7f030052 vibrate=null sound=null defaults=0x0 flags=0x2 kind=[null])  
Native set alarm :Alarm{41e1ca00 type 3 com.kindroid.security}  
reset poweroff alarm none  

-- "time"格式 :
"日期 时间 优先级 / 标签 (进程ID) : 进程名称 : 日志信息 ", 使用adb logcat -v time 命令;

octopus@octopus:~$ adb logcat -v time  
--------- beginning of /dev/log/system  
04-25 17:18:13.019 V/ActivityManager(  500): Broadcast sticky: Intent { act=android.intent.action.SIG_STR flg=0x10 (has extras) } ordered=false userid=-1  
04-25 17:18:13.157 V/NotificationService(  500): enqueueNotificationInternal: pkg=com.kindroid.security id=1020 notification=Notification(pri=0 contentView=com.kindroid.security/0x7f030052 vibrate=null sound=null defaults=0x0 flags=0x2 kind=[null])  
04-25 17:18:13.158 V/NotificationService(  500): notifications are enabled for com.kindroid.security  
04-25 17:18:13.158 V/NotificationService(  500): Assigned score=0 to Notification(pri=0 contentView=com.kindroid.security/0x7f030052 vibrate=null sound=null defaults=0x0 flags=0x2 kind=[null])  
04-25 17:18:13.555 V/ActivityManager(  500): getTasks: max=1, flags=0, receiver=null  

-- "long"格式:
" [ 日期 时间 进程ID : 线程ID 优先级 / 标签] 日志信息 ", 输出以上提到的所有的头信息, 使用adb logcat -v long 命令;

octopus@octopus:~$ adb logcat -v long  
--------- beginning of /dev/log/system  
[ 04-25 17:21:18.118   500:0x2fe V/ActivityManager ]  
We have pending thumbnails: null  
  
[ 04-25 17:21:18.696   593:0x251 W/ActivityThread ]  
Content provider com.android.providers.telephony.TelephonyProvider already published as telephony  
  
[ 04-25 17:21:19.119   500:0x396 V/ActivityManager ]  
getTasks: max=1, flags=0, receiver=null  

清空日志缓存信息 :

使用 adb logcat -c 命令, 可以将之前的日志信息清空, 重新开始输出日志信息;

将缓存日志输出 :

使用 adb logcat -d 命令, 输出命令, 之后推出命令, 不会进行阻塞;

输出最近的日志 :

使用adb logcat -t 5 命令, 可以输出最近的5行日志, 并且不会阻塞;

octopus@octopus:~$ adb logcat -t 5  
--------- beginning of /dev/log/system  
--------- beginning of /dev/log/main  
W/ADB_SERVICES(10028): adb: unable to open /proc/10028/oom_adj  
D/dalvikvm(23292): threadid=11: created from interp  
D/dalvikvm(23292): start new thread  
D/dalvikvm(23292): threadid=11: notify debugger  
D/dalvikvm(23292): threadid=11 (Thread-24538): calling run()  
octopus@octopus:~$   

查看日志缓冲区信息 :

使用 adb logcat -g 命令;

octopus@octopus:~$ adb logcat -g  
/dev/log/main: ring buffer is 256Kb (255Kb consumed), max entry is 5120b, max payload is 4076b  
/dev/log/system: ring buffer is 256Kb (255Kb consumed), max entry is 5120b, max payload is 4076b  
octopus@octopus:~$   

(2) 过滤项解析

过滤项格式 : <tag>[:priority] , 标签:日志等级, 默认的日志过滤项是 " *:I " ;

-- V : Verbose (明细);

-- D : Debug (调试);

-- I : Info (信息);

-- W : Warn (警告);

-- E : Error (错误);

-- F: Fatal (严重错误);

-- S : Silent(Super all output) (最高的优先级, 可能不会记载东西);

  • 过滤指定等级日志 :
    使用 adb logcat 10 *:E 命令, 显示 Error 以上级别的日志;
octopus@octopus:~$ adb logcat *:E  
  
Note: log switch off, only log_main and log_events will have logs!  
--------- beginning of /dev/log/main  
E/WifiHW  (  441): wifi_send_command : SCAN_RESULTS ; interface index=0;  
E/WifiHW  (  441): wifi_send_command : AP_SCAN 1 ; interface index=0;  
E/WifiHW  (  441): wifi_send_command : SCAN_RESULTS ; interface index=0;  
E/dalvikvm(  756): GC_CONCURRENT freed 1809K, 27% free 19489K/26695K, paused 16ms+5ms, total 109ms  
E/WifiHW  (  441): wifi_send_command : SCAN ; interface index=0;  
E/WifiHW  (  441): wifi_send_command : AP_SCAN 1 ; interface index=0;  
E/WifiHW  (  441): wifi_send_command : SCAN_RESULTS ; interface index=0;  
E/dalvikvm(  756): GC_CONCURRENT freed 1820K, 27% free 19490K/26695K, paused 16ms+3ms, total 102ms  
E/WifiHW  (  441): wifi_send_command : AP_SCAN 1 ; interface index=0;  
E/WifiHW  (  441): wifi_send_command : SCAN_RESULTS ; interface index=0;
  • 过滤指定标签等级日志 :
    使用 adb logcat WifiHW:D *:S 命令进行过滤;
    -- 命令含义 :
    输出10条日志, 日志是 标签为 WifiHW, 并且优先级 Debug(调试) 等级以上的级别的日志;

--注意 *:S :
如果没有 *S 就会输出错误;

octopus@octopus:~$ adb logcat WifiHW:D *:S  
  
Note: log switch off, only log_main and log_events will have logs!  
--------- beginning of /dev/log/main  
E/WifiHW  (  441): wifi_send_command : SCAN_RESULTS ; interface index=0;  
E/WifiHW  (  441): wifi_send_command : AP_SCAN 1 ; interface index=0;  
E/WifiHW  (  441): wifi_send_command : SCAN_RESULTS ; interface index=0;  
E/WifiHW  (  441): wifi_send_command : AP_SCAN 1 ; interface index=0;  
E/WifiHW  (  441): wifi_send_command : SCAN_RESULTS ; interface index=0;  
E/WifiHW  (  441): wifi_send_command : AP_SCAN 1 ; interface index=0;  
E/WifiHW  (  441): wifi_send_command : SCAN_RESULTS ; interface index=0;  
  • 可以同时设置多个过滤器 :
    使用adb logcat WifiHW:D dalvikvm:I *:S 命令, 输出 WifiHW 标签 的 Debug 以上级别 和 dalvikvm 标签的 Info 以上级别的日志;
octopus@octopus:~$ adb logcat WifiHW:D dalvikvm:I *:S   
  
Note: log switch off, only log_main and log_events will have logs!  
--------- beginning of /dev/log/main  
E/WifiHW  (  441): wifi_send_command : AP_SCAN 1 ; interface index=0;  
E/WifiHW  (  441): wifi_send_command : SCAN_RESULTS ; interface index=0;  
E/dalvikvm(  756): GC_CONCURRENT freed 1820K, 27% free 19490K/26695K, paused 17ms+2ms, total 110ms  
E/WifiHW  (  441): wifi_send_command : AP_SCAN 1 ; interface index=0;  
E/WifiHW  (  441): wifi_send_command : SCAN_RESULTS ; interface index=0;  
E/WifiHW  (  441): wifi_send_command : AP_SCAN 1 ; interface index=0;  
E/WifiHW  (  441): wifi_send_command : SCAN_RESULTS ; interface index=0;  
E/dalvikvm(  756): GC_CONCURRENT freed 1810K, 27% free 19489K/26695K, paused 17ms+5ms, total 108ms  
E/WifiHW  (  441): wifi_send_command : AP_SCAN 1 ; interface index=0;  
E/WifiHW  (  441): wifi_send_command : SCAN_RESULTS ; interface index=0;  
    1. 使用管道过滤日志
      过滤固定字符串 : 只要命令行出现的日志都可以过滤, 不管是不是标签;

-- 命令 : adb logcat | grep Wifi ;

octopus@octopus:~$ adb logcat | grep Wifi  
E/WifiHW  (  441): wifi_send_command : AP_SCAN 1 ; interface index=0;  
E/WifiHW  (  441): wifi_send_command : SCAN_RESULTS ; interface index=0;  
E/WifiHW  (  441): wifi_send_command : SCAN ; interface index=0;  
E/WifiHW  (  441): wifi_send_command : AP_SCAN 1 ; interface index=0;  
E/WifiHW  (  441): wifi_send_command : SCAN_RESULTS ; interface index=0;  
E/WifiHW  (  441): wifi_send_command : AP_SCAN 1 ; interface index=0;  
E/WifiHW  (  441): wifi_send_command : SCAN_RESULTS ; interface index=0;  
image.png
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 217,826评论 6 506
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,968评论 3 395
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 164,234评论 0 354
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,562评论 1 293
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,611评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,482评论 1 302
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,271评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,166评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,608评论 1 314
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,814评论 3 336
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,926评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,644评论 5 346
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,249评论 3 329
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,866评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,991评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,063评论 3 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,871评论 2 354

推荐阅读更多精彩内容