首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何从Android设备获取日志文件?

如何从Android设备获取日志文件?
EN

Stack Overflow用户
提问于 2010-05-21 21:04:53
回答 9查看 295.7K关注 0票数 140

我想将日志文件从设备拉到我的PC上。我该怎么做呢?

EN

回答 9

Stack Overflow用户

发布于 2014-03-04 22:10:34

我希望这段代码能帮助到一些人。我花了两天时间弄清楚如何从设备上登录,然后过滤它:

代码语言:javascript
复制
public File extractLogToFileAndWeb(){
        //set a file
        Date datum = new Date();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd", Locale.ITALY);
        String fullName = df.format(datum)+"appLog.log";
        File file = new File (Environment.getExternalStorageDirectory(), fullName);

        //clears a file
        if(file.exists()){
            file.delete();
        }


        //write log to file
        int pid = android.os.Process.myPid();
        try {
            String command = String.format("logcat -d -v threadtime *:*");        
            Process process = Runtime.getRuntime().exec(command);

            BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
            StringBuilder result = new StringBuilder();
            String currentLine = null;

            while ((currentLine = reader.readLine()) != null) {
                   if (currentLine != null && currentLine.contains(String.valueOf(pid))) {
                       result.append(currentLine);
                       result.append("\n");
                    }
            }

            FileWriter out = new FileWriter(file);
            out.write(result.toString());
            out.close();

            //Runtime.getRuntime().exec("logcat -d -v time -f "+file.getAbsolutePath());
        } catch (IOException e) {
            Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_SHORT).show();
        }


        //clear the log
        try {
            Runtime.getRuntime().exec("logcat -c");
        } catch (IOException e) {
            Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_SHORT).show();
        }

        return file;
    }

正如@mehdok所指出的

向清单添加读取日志的权限

代码语言:javascript
复制
<uses-permission android:name="android.permission.READ_LOGS" />
票数 32
EN

Stack Overflow用户

发布于 2012-09-30 11:18:43

我会使用这样的东西:

代码语言:javascript
复制
$adb logcat -d > logcat.txt

-d选项将整个循环缓冲区转储到文本文件中,如果您正在查找特定的操作/意图,请尝试

代码语言:javascript
复制
$adb logcat -d | grep 'com.whatever.you.are.looking.for' -B 100 -A 100 > shorterlog.txt

希望这能有所帮助:)

票数 30
EN

Stack Overflow用户

发布于 2016-05-26 20:28:49

对于那些对USB调试或使用adb不感兴趣的人,有一个更简单的解决方案。在Android 6(不确定之前的版本)中,开发者工具下有一个选项: Take Bug Report

单击此选项将准备一份错误报告,并提示您将其保存到驱动器或通过电子邮件发送。

我发现这是获取日志的最简单方法。我不喜欢打开USB调试。

票数 14
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2882253

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档