【问题标题】:How to redirect my log output from logcat to the SD-Card on an android device?如何将我的日志输出从 logcat 重定向到 Android 设备上的 SD 卡?
【发布时间】:2011-03-22 13:00:37
【问题描述】:

我正在尝试将我的应用程序的日志重定向到 sdcard 文件。但我没有这样做。我正在尝试这样的事情。

String cmd= "logcat -v time   ActivityManager:W  myapp:D  *:* >\""+file.getAbsolutePath()+"\"";
Runtime.getRuntime().exec(cmd);

我也尝试了 -f 选项,但它也不起作用。

【问题讨论】:

    标签: android logging


    【解决方案1】:

    这是我的工作版本:

    try {
        File filename = new File(Environment.getExternalStorageDirectory()+"/logfile.log"); 
        filename.createNewFile(); 
        String cmd = "logcat -d -f "+filename.getAbsolutePath();
        Runtime.getRuntime().exec(cmd);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    

    找到您的日志文件 /sdcard/logfile.log

    【讨论】:

    • 我仍然得到一个空文件 logFile.log。我已经添加了 WRITE_EXTERNAL_STORAGE 和 READ_LOGS 权限
    • -d 导致仅写入当前缓冲区。 Logcat 然后停止写入任何其他数据。查看熊的答案以获得连续写作。
    【解决方案2】:

    使用logcat -f <filename> 将其转储到文件系统中的文件中。
    确保您使用的文件名在 SD 卡中,即以 /sdcard/... 开头。

    另外,为了向logcat 程序传递参数,您应该向exec 方法传递一个字符串数组(而不是一个字符串):

    String[] cmd = new String[] { "logcat", "-f", "/sdcard/myfilename", "-v", "time", "ActivityManager:W", "myapp:D" };
    

    最后,如果所有其他方法都失败了,请使用 logcat 的完整路径:/system/bin/logcat 而不仅仅是 logcat

    【讨论】:

    • 感谢回复..我也试过了,但我得到的是空文件
    • 请注意,日志文件显示内容可能需要一些时间,因为它可能正在缓冲对文件的写入(并且一次写入几 KB)
    • 还有一件重要的事情——确保你的应用拥有 WRITE_EXTERNAL_STORAGE 和 READ_LOGS 权限——否则它不会允许它读取日志并将它们写入 sdcard。
    • 请注意,从 Android 4.2 起,常规 SDK 应用程序无法保存 READ_LOGS
    • 如何按包名过滤
    【解决方案3】:
       /**
         * Launches a logcat process.
         * To stop the logcat preserve the use:
         * process.destroy();
         * in the onBackPressed()
         *
         * @param filename destination of logcat output
         * @return Process logcaat is running on
         */
    
        public Process launchLogcat(String filename) {
            Process process = null;
            String cmd = "logcat -f " + filename + "\n";
            try {
                process = Runtime.getRuntime().exec(cmd);
            } catch (IOException e) {
                process = null;
            }
            return process;
        }
    

    【讨论】:

    • 谢谢。但是我应该把这段代码放在哪里?我将如何开始这个过程??
    • 把上面的代码放到你的 main_activity 类中。然后创建一个 private Process 进程并在 onCreate 方法中像这样 pupulate 它:process = launchLogcat("sdcard/log.file");你就完成了!
    【解决方案4】:

    尝试在字符串中的每个元素后使用空格。否则,它将被视为没有空格的单行命令并且什么都不做。

    【讨论】:

    • 嗨,我试过了,它给了我空文件...这是我试过的,文件文件名=新文件(“/sdcard/logfile.log”);文件名.createNewFile(); String[] cmd = new String[] { "logcat", "-v","time", "ActivityManager:W", "myApp:D",":","\"" +filename.getAbsolutePath()+"\""}; Runtime.getRuntime().exec(cmd);
    【解决方案5】:

    如果您得到一个空日志文件,请尝试将文件扩展名从 .log 更改为 .txt

    【讨论】:

      【解决方案6】:
      public static void saveLogInSDCard(Context context){
          String filename = Environment.getExternalStorageDirectory() + File.separator + "project_app.log";
          String command = "logcat -d *:V";
      
          try{
              Process process = Runtime.getRuntime().exec(command);
      
              BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
              String line = null;
              try{
                  File file = new File(filename);
                  file.createNewFile();
                  FileWriter writer = new FileWriter(file);
                  while((line = in.readLine()) != null){
                      writer.write(line + "\n");
                  }
                  writer.flush();
                  writer.close();
              }
              catch(IOException e){
                  e.printStackTrace();
              }
          }
          catch(IOException e){
              e.printStackTrace();
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-06-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-01-10
        • 1970-01-01
        相关资源
        最近更新 更多