6

Recently, I'm developing a android application. I've got a lot of Log.i or Log.e in my code.

When I test the app on my phone, which is connected to my computer via USB, I can get all the logs under the Logcat. But when my phone not connecting to my computer, I got no idea where the bug comes from because Log.* doesn't write logs to file.

Could anyone give my some suggestions on how to redirect the Log.* to a file with minimal change to my code? Thanks a lot!

p.s.

I've searched about how to redirect Logcat to file using adb logcat. Could anyone tell me how to filter the logs which are corresponding to my current app?

Judking
  • 6,111
  • 11
  • 55
  • 84

1 Answers1

5

To save LogCat log to file programmatically on your device use for example this code:

String filePath = Environment.getExternalStorageDirectory() + "/logcat.txt";
Runtime.getRuntime().exec(new String[]{"logcat", "-f", filePath, "MyAppTAG:V", "*:S"});

"MyAppTAG:V" sets the priority level for all tags to Verbose (lowest priority)

"*:S" sets the priority level for all tags to "silent"

If you want to save all logcat just put "*:V".

More information about logcat here.

dthree
  • 19,847
  • 14
  • 77
  • 106
yuralife
  • 1,545
  • 2
  • 21
  • 35
  • I don't quite understand: You are setting both verbose and then silent. What is the difference between the two last arguments? – dthree Nov 24 '17 at 17:51
  • @dthree in this example we set our app tag (e.g. package name) to verbose, and all other logs to silent to ignore them – yuralife Nov 24 '17 at 19:31
  • Okay. I was testing this, it looks like putting `*:S` after the more specific filter might overwrite that filter and make everything silent. Putting the `*:S` earlier fixed this for me. – dthree Nov 24 '17 at 21:26
  • I find some vendor-specific rom will forbid child process to run. Is there some work-around for this problem? – Tshunglee Dec 21 '21 at 08:20