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

Pen*_*m10 122 android

我想将日志文件从设备拉到我的PC.我怎样才能做到这一点?

Mac*_*rse 106

Logcollector是一个不错的选择,但您需要先安装它.

当我想通过邮件发送日志文件时,我通常会执行以下操作:

  • 是不是有任何*简单*方法,只需通过USB作为外部存储设备连接日志文件,只需从设备中提取,无需安装*adb*或类似的东西? (10认同)
  • 请注意,logcollector不适用于4.1以上版本的android,因为现在只允许应用程序读取自己的日志条目.(https://groups.google.com/forum/#!topic/android-log-collector/vgYXYbg-O1U) (5认同)

use*_*692 29

我希望这段代码可以帮助别人.我花了两天时间弄清楚如何从设备登录,然后过滤它:

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;
    }
Run Code Online (Sandbox Code Playgroud)

正如@mehdok指出的那样

将权限添加到清单以读取日志

<uses-permission android:name="android.permission.READ_LOGS" />
Run Code Online (Sandbox Code Playgroud)


小智 25

我会用这种东西:

$adb logcat -d > logcat.txt
Run Code Online (Sandbox Code Playgroud)

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

$adb logcat -d | grep 'com.whatever.you.are.looking.for' -B 100 -A 100 > shorterlog.txt
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助 :)


jjh*_*okk 12

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

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

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


Bra*_*ein 10

编辑:

内部日志是内存中的循环缓冲区.实际上每个都有一些这样的循环缓冲区:radio,events,main.默认为main.

要获得缓冲区的副本,一种技术涉及在设备上执行命令并将输出作为字符串变量获取.

SendLog是一个开源应用程序,它可以做到这一点:http://www.l6n.org/android/sendlog.shtml

关键是要logcat在嵌入式操作系统中的设备上运行.它并不像听起来那么难,只需查看链接中的开源应用程序即可.


neo*_*eye 8

我常常得到错误" logcat read: Invalid argument".在从日志中读取之前,我必须清除日志.

我喜欢这个:

prompt> cd ~/Desktop
prompt> adb logcat -c
prompt> adb logcat | tee log.txt
Run Code Online (Sandbox Code Playgroud)


Pio*_*r Z 6

我知道这是一个老问题,但我相信即使在 2018 年仍然有效。

每个 Android 设备的开发者选项中都有一个隐藏的错误报告选项。

注意:这将转储整个系统日志

如何开启开发者选项?请参阅: https: //developer.android.com/studio/debug/dev-options

什么对我有用:

  1. 重新启动您的设备(为了创建最少的垃圾日志供开发人员分析)
  2. 重现你的错误
  3. 转到“设置”->“开发者选项”->“提交错误报告”
  4. 等待Android系统收集日志(观察通知中的进度条)
  5. 完成后,点击通知进行共享(您可以使用 Gmail 或其他方式)

怎么读这个?打开bugreport-1960-01-01-hh-mm-ss.txt

你可能想寻找这样的东西:

------ SYSTEM LOG (logcat -v threadtime -v printable -d *:v) ------
--------- beginning of crash
06-13 14:37:36.542 19294 19294 E AndroidRuntime: FATAL EXCEPTION: main
Run Code Online (Sandbox Code Playgroud)

或者:

------ SYSTEM LOG (logcat -v threadtime -v printable -d *:v) ------
--------- beginning of main
Run Code Online (Sandbox Code Playgroud)