【问题标题】:Write android logcat data to a file将android logcat数据写入文件
【发布时间】:2011-09-04 17:21:58
【问题描述】:

每当用户想要收集日志时,我想将 Android logcat 转储到一个文件中。通过 adb 工具,我们可以将日志重定向到使用 adb logcat -f filename 的文件,但我如何以编程方式做到这一点?

【问题讨论】:

标签: android logging


【解决方案1】:

这是阅读日志的example

您可以将其更改为写入文件而不是 TextView

需要AndroidManifest的权限:

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

代码:

public class LogTest extends Activity {
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    try {
      Process process = Runtime.getRuntime().exec("logcat -d");
      BufferedReader bufferedReader = new BufferedReader(
      new InputStreamReader(process.getInputStream()));

      StringBuilder log = new StringBuilder();
      String line;
      while ((line = bufferedReader.readLine()) != null) {
        log.append(line);
      }
      TextView tv = (TextView) findViewById(R.id.textView1);
      tv.setText(log.toString());
    } catch (IOException e) {
    }
  }
}

【讨论】:

  • @jkhouw1... +60 我见过的最佳答案和发布答案的好方法,(我也向你学习)这个答案总是有帮助的,即使链接死了,我只需要只添加滚动视图,我已经完成了我想要的,因为你的回答,干杯!!
  • 很好..但是有没有办法根据标签过滤这些日志?
  • 我没有对此进行测试,但我认为您可以将其更改为“logcat -d -s YourTagToFilterOn”
  • 我们怎样才能把包名过滤掉。
  • 此权限已在 Android 4.3 中移除,因此应用程序无法再在其进程之外使用它。此外,许多设备更进一步,这没有任何输出。我没有清单,因为我经常看到它。
【解决方案2】:

Logcat 可以直接写入文件:

public static void saveLogcatToFile(Context context) {    
    String fileName = "logcat_"+System.currentTimeMillis()+".txt";
    File outputFile = new File(context.getExternalCacheDir(),fileName);
    @SuppressWarnings("unused")
    Process process = Runtime.getRuntime().exec("logcat -f "+outputFile.getAbsolutePath());
}

有关 logcat 的更多信息:请参阅 http://developer.android.com/tools/debugging/debugging-log.html

【讨论】:

  • 这个命令似乎停止了,因为 logcat -f 不会自动退出。有什么方法可以将日志写入文件并自动退出类似于 logcat -d 的过程?
  • 尝试在末尾加上 & 使其成为背景。
  • logcat -df 将所有日志接收到 并立即退出。
  • 在最初使用这种方法后,我转而使用缓冲读写器,因为 -f 不能很好地处理文件名中的空格。
  • 是的,应该是-df。另外注意:此代码将其转储到外部缓存目录中
【解决方案3】:

或者你可以试试这个变体

try {
    final File path = new File(
            Environment.getExternalStorageDirectory(), "DBO_logs5");
    if (!path.exists()) {
        path.mkdir();
    }
    Runtime.getRuntime().exec(
            "logcat  -d -f " + path + File.separator
                    + "dbo_logcat"
                    + ".txt");
} catch (IOException e) {
    e.printStackTrace();
}

【讨论】:

    【解决方案4】:
    public static void writeLogToFile(Context context) {    
        String fileName = "logcat.txt";
        File file= new File(context.getExternalCacheDir(),fileName);
        if(!file.exists())
             file.createNewFile();
        String command = "logcat -f "+file.getAbsolutePath();
        Runtime.getRuntime().exec(command);
    }
    

    上述方法会将所有日志写入文件。另外请在 Manifest 文件中添加以下权限

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

    【讨论】:

      猜你喜欢
      • 2013-01-20
      • 1970-01-01
      • 2011-05-07
      • 1970-01-01
      • 1970-01-01
      • 2017-12-20
      • 2017-03-23
      • 2019-12-13
      • 1970-01-01
      相关资源
      最近更新 更多