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

6

19

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.

Delaine answered 29/7, 2010 at 5:28 Comment(0)
B
22

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.

Barmecide answered 29/7, 2010 at 6:7 Comment(5)
thanx for reply..I tried the same but i am getting empty fileDelaine
please note that it may take some time for the logfile to show contents, as it might be buffering writes to the file (and writing a few KB at a time)Barmecide
And another important thing - make sure your app has the WRITE_EXTERNAL_STORAGE and READ_LOGS permissions - or it won't allow it to read logs and write them to the sdcard .Barmecide
And note that regular SDK apps cannot hold READ_LOGS as of Android 4.2 or thereabouts.Ephemera
how to filter by package nameCrenulate
T
26

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

Tarpeia answered 20/1, 2011 at 15:34 Comment(2)
I still get an empty file logFile.log. I already added WRITE_EXTERNAL_STORAGE and READ_LOGS permissionsLodie
-d leads to only writing the current buffer. Logcat then stops to write any additional data. See bear's answer to get continuous writing.Furl
B
22

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.

Barmecide answered 29/7, 2010 at 6:7 Comment(5)
thanx for reply..I tried the same but i am getting empty fileDelaine
please note that it may take some time for the logfile to show contents, as it might be buffering writes to the file (and writing a few KB at a time)Barmecide
And another important thing - make sure your app has the WRITE_EXTERNAL_STORAGE and READ_LOGS permissions - or it won't allow it to read logs and write them to the sdcard .Barmecide
And note that regular SDK apps cannot hold READ_LOGS as of Android 4.2 or thereabouts.Ephemera
how to filter by package nameCrenulate
M
4
   /**
     * Launches a logcat process.
     * To stop the logcat preserve the use:
     * process.destroy();
     * in the onBackPressed()
     *
     * @param filename destination of logcat output
     * @return Process logcaat is running on
     */

    public Process launchLogcat(String filename) {
        Process process = null;
        String cmd = "logcat -f " + filename + "\n";
        try {
            process = Runtime.getRuntime().exec(cmd);
        } catch (IOException e) {
            process = null;
        }
        return process;
    }
Maroon answered 17/7, 2013 at 5:46 Comment(2)
Thank you. But where should I put this code? How will I start the process??Wrench
Put the code above in your main_activity class. Then create a private Process process and in the onCreate method pupulate it like this: process = launchLogcat("sdcard/log.file"); and you are done!Wrench
P
0

Try using a space after every element in the string. Otherwise, it will consider as a single line command without spaces and do nothing.

Pulpboard answered 30/7, 2010 at 3:25 Comment(1)
hi, I tried,its giving me empty file...This is wat i tried, File filename = new File("/sdcard/logfile.log"); filename.createNewFile(); String[] cmd = new String[] { "logcat ", "-v ","time ", "ActivityManager:W ", "myApp:D ",": " ,"\""+filename.getAbsolutePath()+"\""}; Runtime.getRuntime().exec(cmd);Delaine
E
0

If you are getting an empty log file, try change the extension of file from .log to .txt.

Existentialism answered 7/3, 2014 at 9:49 Comment(0)
R
0
public static void saveLogInSDCard(Context context){
    String filename = Environment.getExternalStorageDirectory() + File.separator + "project_app.log";
    String command = "logcat -d *:V";

    try{
        Process process = Runtime.getRuntime().exec(command);

        BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
        String line = null;
        try{
            File file = new File(filename);
            file.createNewFile();
            FileWriter writer = new FileWriter(file);
            while((line = in.readLine()) != null){
                writer.write(line + "\n");
            }
            writer.flush();
            writer.close();
        }
        catch(IOException e){
            e.printStackTrace();
        }
    }
    catch(IOException e){
        e.printStackTrace();
    }
}
Ruskin answered 30/6, 2016 at 12:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.