以编程方式捕获LogCat或将其导出到文件?

Ami*_*oid 5 android

我想过滤一个logcat

String myCommand="logcat -f /sdcard/output.txt"; //no filters, keep writing  
myCommand="logcat -d -f /sdcard/output.txt"; //no filters, just a dump
Run Code Online (Sandbox Code Playgroud)

对我来说工作正常但对mytag没有.

我也在使用代码:

String myCommand="logcat myTag *:S"; //the equivalent of logcat -s myTag  
myCommand="logcat -s myTag:D";   
myCommand="logcat -s myTag:E myTag2:D";  
myCommand="logcat myTag:E myTag2:D";  
Run Code Online (Sandbox Code Playgroud)

但它返回空文件.

小智 10

try {
   File filename = new File(Environment.getExternalStorageDirectory()+"/gphoto4.html"); 
        filename.createNewFile(); 
        String cmd = "logcat -d -f "+filename.getAbsolutePath();
        Runtime.getRuntime().exec(cmd);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
Run Code Online (Sandbox Code Playgroud)

也用

String cmd = "logcat -v time -r 100 -f <filename> [TAG]:I [MyApp]:D *:S";
Runtime.getRuntime().exec(cmd);


-v -> Sets the output format for log messages.
-r -> for specifying the size of file.
-f -> file to which you want to write the logs.
[TAG] -> Tag of your application's log.
[MyApp] -> Your application name.
Run Code Online (Sandbox Code Playgroud)

  • 有关进一步的 logcat 可执行参数,请参阅 [logcat 命令行工具](https://developer.android.com/studio/command-line/logcat.html) (3认同)

Ami*_*oid 8

File filename = new File(Environment.getExternalStorageDirectory()+"/mylog.log"); 
filename.createNewFile(); 
String cmd = "logcat -d -f"+filename.getAbsolutePath();
Runtime.getRuntime().exec(cmd);
Run Code Online (Sandbox Code Playgroud)

这个对我有用.但是对于所有logcat输出而不是特殊标签(mytag).

  • &lt;uses-permission android:name="android.permission.READ_LOGS" /&gt; &lt;uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /&gt; (3认同)