当前位置:操作系统 > 安卓/Android >>

如何获取 android 的系统日志

 

公司要调试产品,想要监视试用品的logcat ,捕获本机产生的日志,发送到服务器上

 

 

 

折腾了一天,基本上做好

 

现在来看代码

 

  读取日志需要的权限

 

<uses-permission android:name="android.permission.READ_LOGS"/>

主要代码

 

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            {                ArrayList<String> cmdLine=new ArrayList<String>();   //设置命令   logcat -d 读取日志                cmdLine.add("logcat");                cmdLine.add("-d");                                ArrayList<String> clearLog=new ArrayList<String>();  //设置命令  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--------");        }    }}

这里比较令人纠结的一点就是日志的清理  logcat -c  如果不加入 清理 在buffer满为止,代码自身能够迭代6~7次....

 

 

 

接下来看看结果         日志比较长....一般都这样..原日志用蓝色,捕获并输出对比部分为红色

 

11-07 06:32:35.895: DEBUG/AndroidRuntime(1071): >>>>>>>>>>>>>> AndroidRuntime START <<<<<<<<<<<<<<11-07 06:32:35.905: DEBUG/AndroidRuntime(1071): CheckJNI is ON11-07 06:32:36.106: DEBUG/AndroidRuntime(1071): --- registering native functions ---11-07 06:32:36.436: DEBUG/ddm-heap(1071): Got feature list request11-07 06:32:36.817: DEBUG/PackageParser(52): Scanning package: /data/app/vmdl53483.tmp11-07 06:32:36.915: INFO/PackageManager(52): Removing non-system package:mt.fzgh11-07 06:32:36.915: DEBUG/PackageManager(52): Removing package mt.fzgh11-07 06:32:36.926: DEBUG/PackageManager(52):   Activities: mt.fzgh.LogDemoActivity11-07 06:32:37.025: DEBUG/PackageManager(52): Scanning package mt.fzgh11-07 06:32:37.037: INFO/PackageManager(52): /data/app/vmdl53483.tmp changed; unpacking11-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 1ms11-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.LogDemoActivity11-07 06:32:37.296: DEBUG/ActivityManager(52): Uninstalling process mt.fzgh11-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: 911-07 06:32:37.336: DEBUG/ActivityManager(52): Received spurious death notification for thread android.os.BinderProxy@44ce758811-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.dex11-07 06:32:37.426: DEBUG/PackageManager(52): New package installed in /data/app/mt.fzgh.apk11-07 06:32:37.525: DEBUG/AndroidRuntime(1071): Shutting down VM11-07 06:32:37.525: DEBUG/dalvikvm(1071): DestroyJavaVM waiting for non-daemon threads to exit11-07 06:32:37.535: DEBUG/dalvikvm(1071): DestroyJavaVM shutting VM down11-07 06:32:37.535: DEBUG/dalvikvm(1071): HeapWorker thread shutting down11-07 06:32:37.535: DEBUG/dalvikvm(1071): HeapWorker thread has shut down11-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 entries11-07 06:32:37.535: DEBUG/dalvikvm(1071): VM cleaning up11-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.fzgh11-07 06:32:37.575: ERROR/AndroidRuntime(1071): ERROR: thread attach failed11-07 06:32:38.047: DEBUG/dalvikvm(52): GC freed 13838 objects / 811832 bytes in 263ms11-07 06:32:38.056: WARN/ResourceType(52): Re

补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,