Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to redirect my log output from logcat to the SD-Card on an android device?

I'm trying to redirect the log of my app to the sdcard file. But i failed to do so. I'm trying something like this.

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

I tried the -f option also but it is not working either.

like image 243
MGK Avatar asked Jul 29 '10 05:07

MGK


People also ask

Where are Logcat logs stored?

They are stored as circular memory buffers on the device. If you run "adb logcat > myfile" on your host system, you can retrieve the content into a file.

How do I access system logs on Android?

Android 4.0 and older You can either download the SDK and use adb logcat or get Logcat Extrem from the Google Play Store, which shows the log directly on your phone. Alternatively, you can use Terminal Emulator with command "logcat > /sdcard/log. txt" for continuous writing of the log to a file on the SD Card.

Which Android API class will allow you to output messages to Logcat?

The Log class allows you to create log messages that appear in logcat.


2 Answers

This is my worked version:

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();
}

find your logfile /sdcard/logfile.log

like image 83
broderix Avatar answered Oct 15 '22 17:10

broderix


use logcat -f <filename> in order to dump it to a file in the filesystem.
Make sure the filename you're using is in the sdcard, i.e. starts with /sdcard/....

Also, in order to pass arguments to the logcat program, you should pass the exec method an array of strings (and not one string):

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

Finally, if all else fails, use the full path for logcat: /system/bin/logcat instead of just logcat.

like image 41
adamk Avatar answered Oct 15 '22 15:10

adamk