公司要调试产品,想要监视试用品的 logcat ,捕获本机产生的日志,发送到服务器上
(提供一个完整的代码下载 这里会比下面的代码更完善,多了网络传输和格式转换 需要2分哦..不嫌麻烦的话在下面拷贝也行.... )
折腾了一天,基本上做好
现在来看代码
读取日志需要的权限
package mt.fzgh; import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.ArrayList; public class MyLog { public static class MLog //静态类 { public static void getLog() { System.out.println("--------func start--------"); // 方法启动 try { ArrayListcmdLine=new ArrayList (); //设置命令 logcat -d 读取日志 cmdLine.add("logcat"); cmdLine.add("-d"); ArrayList clearLog=new ArrayList (); //设置命令 logcat -c 清除日志 clearLog.add("logcat"); clearLog.add("-c"); Process process=Runtime.getRuntime().exec(cmdLine.toArray(new String[cmdLine.size()])); //捕获日志 BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(process.getInputStream())); //将捕获内容转换为BufferedReader // Runtime.runFinalizersOnExit(true); String str=null; while((str=bufferedReader.readLine())!=null) //开始读取日志,每次读取一行 { Runtime.getRuntime().exec(clearLog.toArray(new String[clearLog.size()])); //清理日志....这里至关重要,不清理的话,任何操作都将产生新的日志,代码进入死循环,直到bufferreader满 System.out.println(str); //输出,在logcat中查看效果,也可以是其他操作,比如发送给服务器.. } if(str==null) { System.out.println("-- is null --"); } } catch(Exception e) { e.printStackTrace(); } System.out.println("--------func end--------"); } } }
接下来看看结果 日志比较长....一般都这样..原日志用蓝色,捕获并输出对比部分为红色
11-07 06:32:35.895: DEBUG/AndroidRuntime(1071): >>>>>>>>>>>>>> AndroidRuntime START <<<<<<<<<<<<<< 11-07 06:32:35.905: DEBUG/AndroidRuntime(1071): CheckJNI is ON 11-07 06:32:36.106: DEBUG/AndroidRuntime(1071): --- registering native functions --- 11-07 06:32:36.436: DEBUG/ddm-heap(1071): Got feature list request 11-07 06:32:36.817: DEBUG/PackageParser(52): Scanning package: /data/app/vmdl53483.tmp 11-07 06:32:36.915: INFO/PackageManager(52): Removing non-system package:mt.fzgh 11-07 06:32:36.915: DEBUG/PackageManager(52): Removing package mt.fzgh 11-07 06:32:36.926: DEBUG/PackageManager(52): Activities: mt.fzgh.LogDemoActivity 11-07 06:32:37.025: DEBUG/PackageManager(52): Scanning package mt.fzgh 11-07 06:32:37.037: INFO/PackageManager(52): /data/app/vmdl53483.tmp changed; unpacking 11-07 06:32:37.056: DEBUG/installd(32): DexInv: --- BEGIN '/data/app/vmdl53483.tmp' --- 11-07 06:32:37.257: DEBUG/dalvikvm(1077): DexOpt: load 32ms, verify 70ms, opt 1ms 11-07 06:32:37.275: DEBUG/installd(32): DexInv: --- END '/data/app/vmdl53483.tmp' (success) --- 11-07 06:32:37.275: DEBUG/PackageManager(52): Activities: mt.fzgh.LogDemoActivity 11-07 06:32:37.296: DEBUG/ActivityManager(52): Uninstalling process mt.fzgh 11-07 06:32:37.296: DEBUG/ActivityManager(52): Force removing process ProcessRecord{44ed5a28 929:mt.fzgh/10029} (mt.fzgh/10029) 11-07 06:32:37.296: INFO/Process(52): Sending signal. PID: 929 SIG: 9 11-07 06:32:37.336: DEBUG/ActivityManager(52): Received spurious death notification for thread android.os.BinderProxy@44ce7588 11-07 06:32:37.415: INFO/installd(32): move /data/dalvik-cache/data@app@vmdl53483.tmp@classes.dex -> /data/dalvik-cache/data@app@mt.fzgh.apk@classes.dex 11-07 06:32:37.426: DEBUG/PackageManager(52): New package installed in /data/app/mt.fzgh.apk 11-07 06:32:37.525: DEBUG/AndroidRuntime(1071): Shutting down VM 11-07 06:32:37.525: DEBUG/dalvikvm(1071): DestroyJavaVM waiting for non-daemon threads to exit 11-07 06:32:37.535: DEBUG/dalvikvm(1071): DestroyJavaVM shutting VM down 11-07 06:32:37.535: DEBUG/dalvikvm(1071): HeapWorker thread shutting down 11-07 06:32:37.535: DEBUG/dalvikvm(1071): HeapWorker thread has shut down 11-07 06:32:37.535: DEBUG/jdwp(1071): JDWP shutting down net... 11-07 06:32:37.535: INFO/dalvikvm(1071): Debugger has detached; object registry had 1 entries 11-07 06:32:37.535: DEBUG/dalvikvm(1071): VM cleaning up 11-07 06:32:37.546: DEBUG/dalvikvm(1071): LinearAlloc 0x0 used 623916 of 5242880 (11%) 11-07 06:32:37.575: DEBUG/ActivityManager(52): Uninstalling process mt.fzgh 11-07 06:32:37.575: ERROR/AndroidRuntime(1071): ERROR: thread attach failed 11-07 06:32:38.047: DEBUG/dalvikvm(52): GC freed 13838 objects / 811832 bytes in 263ms 11-07 06:32:38.056: WARN/ResourceType(52): Resources don't contain package for resource number 0x7f0700e5 11-07 06:32:38.086: WARN/ResourceType(52): Resources don't contain package for resource number 0x7f020031 11-07 06:32:38.086: WARN/ResourceType(52): Resources don't contain package for resource number 0x7f020030 11-07 06:32:38.086: WARN/ResourceType(52): Resources don't contain package for resource number 0x7f050000 11-07 06:32:38.195: WARN/ResourceType(52): Resources don't contain package for resource number 0x7f060000 11-07 06:32:38.195: WARN/ResourceType(52): Resources don't contain package for resource number 0x7f060001 11-07 06:32:38.395: DEBUG/dalvikvm(106): GC freed 217 objects / 9344 bytes in 319ms 11-07 06:32:38.466: WARN/ResourceType(52): Resources don't contain package for resource number 0x7f0700e5 11-07 06:32:38.466: WARN/ResourceType(52): Resources don't contain package for resource number 0x7f020031 11-07 06:32:38.466: WARN/ResourceType(52): Resources don't contain package for resource number 0x7f020030 11-07 06:32:38.466: WARN/ResourceType(52): Resources don't contain package for resource number 0x7f050000 11-07 06:32:38.475: WARN/ResourceType(52): Resources don't contain package for resource number 0x7f060000 11-07 06:32:38.475: WARN/ResourceType(52): Resources don't contain package for resource number 0x7f060001 11-07 06:32:38.766: DEBUG/AndroidRuntime(1082): >>>>>>>>>>>>>> AndroidRuntime START <<<<<<<<<<<<<< 此处和上面有重复,不过是日志本身重复,上面失败了 11-07 06:32:38.775: DEBUG/AndroidRuntime(1082): CheckJNI is ON 11-07 06:32:39.195: DEBUG/AndroidRuntime(1082): --- registering native functions --- 11-07 06:32:39.535: DEBUG/ddm-heap(1082): Got feature list request 11-07 06:32:39.895: INFO/ActivityManager(52): Starting activity: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=mt.fzgh/.LogDemoActivity } 11-07 06:32:39.985: INFO/ActivityManager(52): Start proc mt.fzgh for activity mt.fzgh/.LogDemoActivity: pid=1088 uid=10029 gids={1007} 11-07 06:32:39.995: DEBUG/AndroidRuntime(1082): Shutting down VM 11-07 06:32:39.995: DEBUG/dalvikvm(1082): DestroyJavaVM waiting for non-daemon threads to exit 11-07 06:32:39.995: DEBUG/dalvikvm(1082): DestroyJavaVM shutting VM down 11-07 06:32:39.995: DEBUG/dalvikvm(1082): HeapWorker thread shutting down 11-07 06:32:39.995: DEBUG/dalvikvm(1082): HeapWorker thread has shut down 11-07 06:32:39.995: DEBUG/jdwp(1082): JDWP shutting down net... 11-07 06:32:39.995: INFO/dalvikvm(1082): Debugger has detached; object registry had 1 entries 11-07 06:32:40.018: DEBUG/dalvikvm(1082): VM cleaning up 11-07 06:32:40.036: DEBUG/dalvikvm(1082): LinearAlloc 0x0 used 639500 of 5242880 (12%) 11-07 06:32:40.065: ERROR/AndroidRuntime(1082): ERROR: thread attach failed 11-07 06:32:40.767: DEBUG/ddm-heap(1088): Got feature list request 11-07 06:32:40.815: INFO/mt(1088): success 11-07 06:32:40.815: INFO/System.out(1088): --------func start-------- 注意这里 此处之下,就是我们捕获到的内容 11-07 06:32:41.086: INFO/System.out(1088): D/AndroidRuntime( 1071): >>>>>>>>>>>>>> AndroidRuntime START <<<<<<<<<<<<<< 11-07 06:32:41.125: INFO/System.out(1088): D/AndroidRuntime( 1071): CheckJNI is ON 11-07 06:32:41.166: INFO/System.out(1088): D/AndroidRuntime( 1071): --- registering native functions --- 11-07 06:32:41.206: INFO/System.out(1088): D/ddm-heap( 1071): Got feature list request 11-07 06:32:41.246: INFO/System.out(1088): D/PackageParser( 52): Scanning package: /data/app/vmdl53483.tmp 11-07 06:32:41.305: INFO/System.out(1088): I/PackageManager( 52): Removing non-system package:mt.fzgh 11-07 06:32:41.346: INFO/System.out(1088): D/PackageManager( 52): Removing package mt.fzgh 11-07 06:32:41.386: INFO/System.out(1088): D/PackageManager( 52): Activities: mt.fzgh.LogDemoActivity 11-07 06:32:41.416: INFO/System.out(1088): D/PackageManager( 52): Scanning package mt.fzgh 11-07 06:32:41.456: INFO/System.out(1088): I/PackageManager( 52): /data/app/vmdl53483.tmp changed; unpacking 11-07 06:32:41.506: INFO/System.out(1088): D/installd( 32): DexInv: --- BEGIN '/data/app/vmdl53483.tmp' --- 11-07 06:32:41.546: INFO/System.out(1088): D/dalvikvm( 1077): DexOpt: load 32ms, verify 70ms, opt 1ms 11-07 06:32:41.586: INFO/System.out(1088): D/installd( 32): DexInv: --- END '/data/app/vmdl53483.tmp' (success) --- 11-07 06:32:41.626: INFO/System.out(1088): D/PackageManager( 52): Activities: mt.fzgh.LogDemoActivity 11-07 06:32:41.666: INFO/System.out(1088): D/ActivityManager( 52): Uninstalling process mt.fzgh 11-07 06:32:41.716: INFO/System.out(1088): D/ActivityManager( 52): Force removing process ProcessRecord{44ed5a28 929:mt.fzgh/10029} (mt.fzgh/10029) 11-07 06:32:41.756: INFO/System.out(1088): I/Process ( 52): Sending signal. PID: 929 SIG: 9 11-07 06:32:41.805: INFO/System.out(1088): D/ActivityManager( 52): Received spurious death notification for thread android.os.BinderProxy@44ce7588 11-07 06:32:41.845: INFO/System.out(1088): I/installd( 32): move /data/dalvik-cache/data@app@vmdl53483.tmp@classes.dex -> /data/dalvik-cache/data@app@mt.fzgh.apk@classes.dex 11-07 06:32:41.887: INFO/System.out(1088): D/PackageManager( 52): New package installed in /data/app/mt.fzgh.apk 11-07 06:32:41.926: INFO/System.out(1088): D/AndroidRuntime( 1071): Shutting down VM 11-07 06:32:41.966: INFO/System.out(1088): D/dalvikvm( 1071): DestroyJavaVM waiting for non-daemon threads to exit 11-07 06:32:42.016: INFO/System.out(1088): D/dalvikvm( 1071): DestroyJavaVM shutting VM down 11-07 06:32:42.056: INFO/System.out(1088): D/dalvikvm( 1071): HeapWorker thread shutting down 11-07 06:32:42.095: INFO/System.out(1088): D/dalvikvm( 1071): HeapWorker thread has shut down 11-07 06:32:42.136: INFO/System.out(1088): D/jdwp ( 1071): JDWP shutting down net... 11-07 06:32:42.176: INFO/System.out(1088): I/dalvikvm( 1071): Debugger has detached; object registry had 1 entries 11-07 06:32:42.216: INFO/System.out(1088): D/dalvikvm( 1071): VM cleaning up 11-07 06:32:42.256: INFO/System.out(1088): D/dalvikvm( 1071): LinearAlloc 0x0 used 623916 of 5242880 (11%) 11-07 06:32:42.296: INFO/System.out(1088): D/ActivityManager( 52): Uninstalling process mt.fzgh 11-07 06:32:42.336: INFO/System.out(1088): E/AndroidRuntime( 1071): ERROR: thread attach failed 11-07 06:32:42.376: INFO/System.out(1088): D/dalvikvm( 52): GC freed 13838 objects / 811832 bytes in 263ms 11-07 06:32:42.416: INFO/System.out(1088): W/ResourceType( 52): Resources don't contain package for resource number 0x7f0700e5 11-07 06:32:42.458: INFO/System.out(1088): W/ResourceType( 52): Resources don't contain package for resource number 0x7f020031 11-07 06:32:42.496: INFO/System.out(1088): W/ResourceType( 52): Resources don't contain package for resource number 0x7f020030 11-07 06:32:42.536: INFO/System.out(1088): W/ResourceType( 52): Resources don't contain package for resource number 0x7f050000 11-07 06:32:42.575: INFO/System.out(1088): W/ResourceType( 52): Resources don't contain package for resource number 0x7f060000 11-07 06:32:42.626: INFO/System.out(1088): W/ResourceType( 52): Resources don't contain package for resource number 0x7f060001 11-07 06:32:42.667: INFO/System.out(1088): D/dalvikvm( 106): GC freed 217 objects / 9344 bytes in 319ms 11-07 06:32:42.706: INFO/System.out(1088): W/ResourceType( 52): Resources don't contain package for resource number 0x7f0700e5 11-07 06:32:42.746: INFO/System.out(1088): W/ResourceType( 52): Resources don't contain package for resource number 0x7f020031 11-07 06:32:42.786: INFO/System.out(1088): W/ResourceType( 52): Resources don't contain package for resource number 0x7f020030 11-07 06:32:42.825: INFO/System.out(1088): W/ResourceType( 52): Resources don't contain package for resource number 0x7f050000 11-07 06:32:42.865: INFO/System.out(1088): W/ResourceType( 52): Resources don't contain package for resource number 0x7f060000 11-07 06:32:42.905: INFO/System.out(1088): W/ResourceType( 52): Resources don't contain package for resource number 0x7f060001 11-07 06:32:42.946: INFO/System.out(1088): D/AndroidRuntime( 1082): 11-07 06:32:42.985: INFO/System.out(1088): D/AndroidRuntime( 1082): >>>>>>>>>>>>>> AndroidRuntime START <<<<<<<<<<<<<< 11-07 06:32:43.036: INFO/System.out(1088): D/AndroidRuntime( 1082): CheckJNI is ON 11-07 06:32:43.095: INFO/System.out(1088): D/AndroidRuntime( 1082): --- registering native functions --- 11-07 06:32:43.139: INFO/System.out(1088): D/ddm-heap( 1082): Got feature list request 11-07 06:32:43.176: INFO/System.out(1088): I/ActivityManager( 52): Starting activity: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=mt.fzgh/.LogDemoActivity } 11-07 06:32:43.216: INFO/System.out(1088): I/ActivityManager( 52): Start proc mt.fzgh for activity mt.fzgh/.LogDemoActivity: pid=1088 uid=10029 gids={1007} 11-07 06:32:43.256: INFO/System.out(1088): D/AndroidRuntime( 1082): Shutting down VM 11-07 06:32:43.297: INFO/System.out(1088): D/dalvikvm( 1082): DestroyJavaVM waiting for non-daemon threads to exit 11-07 06:32:43.336: INFO/System.out(1088): D/dalvikvm( 1082): DestroyJavaVM shutting VM down 11-07 06:32:43.385: INFO/System.out(1088): D/dalvikvm( 1082): HeapWorker thread shutting down 11-07 06:32:43.426: INFO/System.out(1088): D/dalvikvm( 1082): HeapWorker thread has shut down 11-07 06:32:43.475: INFO/System.out(1088): D/jdwp ( 1082): JDWP shutting down net... 11-07 06:32:43.508: INFO/System.out(1088): I/dalvikvm( 1082): Debugger has detached; object registry had 1 entries 11-07 06:32:43.546: INFO/System.out(1088): D/dalvikvm( 1082): VM cleaning up 11-07 06:32:43.586: INFO/System.out(1088): D/dalvikvm( 1082): LinearAlloc 0x0 used 639500 of 5242880 (12%) 11-07 06:32:43.635: INFO/System.out(1088): E/AndroidRuntime( 1082): ERROR: thread attach failed 11-07 06:32:43.685: INFO/System.out(1088): D/ddm-heap( 1088): Got feature list request 11-07 06:32:43.716: INFO/System.out(1088): I/mt ( 1088): success 11-07 06:32:43.766: INFO/System.out(1088): I/System.out( 1088): --------func start-------- 眼尖的读者估计看到这行了..还是被重复打印出来了,但是我也始终没办法了....就这两行,算了吧,我接受这个冗余,如果各位读者有办法解决...欢迎交流 11-07 06:32:43.805: INFO/System.out(1088): I/global ( 1088): Default buffer size used in BufferedReader constructor. It would be better to be explicit if an 8k-char buffer is required. 11-07 06:32:43.805: INFO/System.out(1088): -- is null -- 11-07 06:32:43.805: INFO/System.out(1088): --------func end-------- 此处捕获的内容输出结束.... 11-07 06:32:44.006: INFO/ActivityManager(52): Displayed activity mt.fzgh/.LogDemoActivity: 4038 ms (total 4038 ms) 11-07 06:32:49.196: DEBUG/dalvikvm(227): GC freed 43 objects / 2048 bytes in 126ms 11-07 06:32:54.236: DEBUG/dalvikvm(106): GC freed 2281 objects / 132056 bytes in 151ms
最后,感谢一下诸多前辈对编码经验的无私奉献....
这里指出一些本人参考过的代码和不足,csdn专家 xys 前辈也制作了 捕获log的demo程序,并且广为流传,不过经过亲测,似乎只能捕获 触发捕获事件 本身,
也是因为这个原因,我才觉得需要一个更加完善的程序来分享给大家,特发此文共勉。
附带一份logcat的 命令...不过好像 过滤器 指令有问题....慎用
选项 | 说明 |
-s | 默认设置过滤器 |
- f 文件 | 输出到日志文件 |
-c | 清除日志 |
-d | 获取日志 |
-g | 获取日志的大小 |
- v 格式 | 设置日志(见下面的格式打印格式) |
- v 格式 | 例 |
brief | W/tag ( 876): message |
process | W( 876) message (tag) |
tag | W/tag : message |
thread | W( 876:0x37c) message |
raw | message |
time | 09-08 05:40:26.729 W/tag ( 876): message |
threadtime | 09-08 05:40:26.729 876 892 W tag : message |
long | [ 09-08 05:40:26.729 876:0x37c W/tag ] message |