0

I tried to find this but couldn't find and results. Apologies if this is a duplicate.

I would like to have android logs written to a file. But i do not wish them to be written to the file in the end, rather as the log statement is executed, it should be written into this file.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Sunny
  • 7,444
  • 22
  • 63
  • 104
  • Did you checked this answer ? http://stackoverflow.com/a/3359857/2065418 – Damien R. Nov 20 '13 at 13:11
  • I looked at it and the difference is that I don;t want to get the logs in one go. I would like to have the logs redirected to a log file as the various log statements are executed. Thanks – Sunny Nov 20 '13 at 13:14
  • [`logback-android`](http://tony19.github.io/logback-android/index.html) supports a `FileAppender` that writes log statements to a file. – tony19 Nov 21 '13 at 02:40

3 Answers3

0

But i do not wish them to be written to the file in the end, rather as the log statement is executed, it should be written into this file.

Then write one or more methods that log to a file, possibly in addition to logging to LogCat. Logging to files has been done in Java development for ~15 years, and there is plenty of code available to demonstrate it.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • u mean a totally new logger? i was actually hoping to use android logger and then just moving them to a file – Sunny Nov 20 '13 at 13:15
  • @Sunny: "u mean a totally new logger?" -- yes. "i was actually hoping to use android logger and then just moving them to a file" -- I was hoping that you would go with a reliable and support solution. – CommonsWare Nov 20 '13 at 13:17
  • that makes sense actually....so using android logger is out...btw is there no lib available which does this? just want to be sure before i write the new logger and realised that there was some external lib which i could have used. thanks – Sunny Nov 20 '13 at 13:20
  • @Sunny: "btw is there no lib available which does this?" -- somebody may well have written a logging framework that supports LogCat as one of several possible stores for the logs. I have not looked for one, though I seem to recall seeing one once. – CommonsWare Nov 20 '13 at 13:22
0

You can try this code!

public static void printLog(Context context){
String filename = context.getExternalFilesDir(null).getPath() + File.separator + "my_app.log";
String command = "logcat -d *:V";

Log.d(TAG, "command: " + command);

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

Ref: How to keep on appending logs while writing into File in SDCard?

Community
  • 1
  • 1
Ami Patel
  • 296
  • 1
  • 13
  • 1
    this would work only as a one time thing.. and not each time the log statement is executed – Sunny Nov 20 '13 at 13:33
0

i use this:

Logger logger=Logger.getLogger("myLoggerName");
logger.setLevel(Level.ALL);
File logFileDirectory=new File(getExternalFilesDir(null),"myLogDirectory");
addFileHandler(logger,logFileDirectory,"myLogFile");
Ray Tayek
  • 9,841
  • 8
  • 50
  • 90