How to redirect the content in Logcat to file on android?
Asked Answered
T

1

7

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?

Thaw answered 11/4, 2013 at 2:38 Comment(1)
Check: #8417889Cornetist
S
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.

Sheathbill answered 20/12, 2013 at 15:46 Comment(4)
I don't quite understand: You are setting both verbose and then silent. What is the difference between the two last arguments?Schuh
@Schuh in this example we set our app tag (e.g. package name) to verbose, and all other logs to silent to ignore themSheathbill
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.Schuh
I find some vendor-specific rom will forbid child process to run. Is there some work-around for this problem?Sumba

© 2022 - 2024 — McMap. All rights reserved.