代码之家  ›  专栏  ›  技术社区  ›  Pentium10

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

  •  116
  • Pentium10  · 技术社区  · 14 年前

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

    13 回复  |  直到 6 年前
        1
  •  103
  •   Andrzej Budzanowski    9 年前

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

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

        2
  •  27
  •   Shubhank    10 年前

    我希望这段代码能帮助别人。我花了2天的时间来了解如何从设备登录,然后对其进行筛选:

    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所指

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

    <uses-permission android:name="android.permission.READ_LOGS" />
    
        3
  •  23
  •   Rohit    12 年前

    我会用这种方法:

    $adb logcat -d > logcat.txt
    

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

    $adb logcat -d | grep 'com.whatever.you.are.looking.for' -B 100 -A 100 > shorterlog.txt
    

    希望这有帮助:)

        4
  •  12
  •   dreamwagon    8 年前

    对于那些不感兴趣的USB调试或使用 adb 有一个更简单的解决方案。在android 6中(不确定以前的版本),在developer tools下有一个选项:take bug report

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

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

        5
  •  10
  •   Brad Hein    14 年前

    编辑:

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

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

    sendlog是一个开源应用程序,它只执行以下操作: http://www.l6n.org/android/sendlog.shtml

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

        6
  •  7
  •   neoneye    13 年前

    我经常会出错” logcat read: Invalid argument “。在从日志中读取之前,我必须清除日志。

    我喜欢这样:

    prompt> cd ~/Desktop
    prompt> adb logcat -c
    prompt> adb logcat | tee log.txt
    
        7
  •  4
  •   Richard Le Mesurier    10 年前

    一个简单的方法是创建自己的日志收集器方法,甚至是市场上现有的日志收集器应用程序。

    对于我的应用程序,我做了一个报告功能,它将日志发送到我的电子邮件(甚至发送到其他地方——一旦你得到了日志,无论你想用它做什么都可以)。

    下面是一个关于如何从设备获取日志文件的简单示例:

        8
  •  3
  •   Weston Ganger    8 年前

    Simple只需运行以下命令将输出获取到终端:

    adb shell logcat
    
        9
  •  2
  •   jfcogato    11 年前

    多亏了用户1354692,我只需要一条线就可以让它变得更简单!他所评论的:

    try {
        File file = new File(Environment.getExternalStorageDirectory(), String.valueOf(System.currentTimeMillis()));
        Runtime.getRuntime().exec("logcat -d -v time -f " + file.getAbsolutePath());}catch (IOException e){}
    
        10
  •  2
  •   lm2a    9 年前

    我创建了一个小库(.aar),通过电子邮件检索日志。你可以在Gmail账户中使用它。这很简单,但很有效。你可以得到一份副本 from here

    该网站是西班牙语的,但有一个pdf和一个英文版的产品描述。

    我希望能有所帮助。

        11
  •  1
  •   Community CDub    7 年前

    两个步骤:

    • 生成日志
    • 加载gmail以发送日志

    .

    1. 生成日志

      File generateLog() {
          File logFolder = new File(Environment.getExternalStorageDirectory(), "MyFolder");
          if (!logFolder.exists()) {
              logFolder.mkdir();
          }
          String filename = "myapp_log_" + new Date().getTime() + ".log";
      
          File logFile = new File(logFolder, filename);
      
          try {
              String[] cmd = new String[] { "logcat", "-f", logFile.getAbsolutePath(), "-v", "time", "ActivityManager:W", "myapp:D" };
              Runtime.getRuntime().exec(cmd);
              Toaster.shortDebug("Log generated to: " + filename);
              return logFile;
          }
          catch (IOException ioEx) {
              ioEx.printStackTrace();
          }
      
          return null;
      }
      
    2. 加载gmail以发送日志

      File logFile = generateLog();
      Intent intent = new Intent(Intent.ACTION_SEND);
      intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
      intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(logFile));
      intent.setType("multipart/");
      startActivity(intent);
      

    1的参考文献

    ~~

    对于2-对于如何加载要查看和发送的日志文件,有许多不同的答案。最后,这里的解决方案实际上对以下两个方面都有效:

    • 加载gmail作为选项
    • 成功附加文件

    非常感谢 https://stackoverflow.com/a/22367055/2162226 对于正确工作的答案

        12
  •  1
  •   Piotr Z    6 年前

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

    有一个选择 做一个错误报告 隐藏在 开发人员选项 在每个Android设备中。

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

    如何启用开发人员选项?见: https://developer.android.com/studio/debug/dev-options

    什么对我有效:

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

    怎么读这个? 打开 错误报告-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
    

    或:

    ------ SYSTEM LOG (logcat -v threadtime -v printable -d *:v) ------
    --------- beginning of main
    
        13
  •  0
  •   Aqib Mumtaz    9 年前

    首先,通过设置android sdk平台工具的路径,确保adb命令是可执行的:

    export PATH=/Users/espireinfolabs/Desktop/soft/android-sdk-mac_x86/platform-tools:$PATH
    

    然后运行:

    adb shell logcat > log.txt
    

    或先转到ADB平台工具:

    cd /Users/user/Android/Tools/android-sdk-macosx/platform-tools 
    

    然后运行

    ./adb shell logcat > log.txt