Fdde F
Fdde F

Reputation: 123

How to save logs to file?

Currently, I'm developing an app for the company I work for, a lot of my co-workers use my app, the thing is, some times crashes/errors occur and I have no way of checking the log messages so I'll like to save the log messages in a text file so when an error happens I can check the log file and get a better idea of what is causing the problem.

I don't need the log to be sent to me through e-mail, it is good enough to have the file locally on the phone so I can plug it in my pc and extract the log file.

Currently, this is how I manage my logs:


try {
        //stuff...
    }
catch (Exception ex) {
        Log.e("Error", ex.getMessage());
    }

The Log.e("Error", ex.getMessage()); is what I would like to save.

Upvotes: 1

Views: 1541

Answers (2)

Rui Liu
Rui Liu

Reputation: 51

If you do not want to change your app, you can try mmlog tool. https://github.com/liurui-1/mmlog

Upvotes: 0

Farzad Kamali
Farzad Kamali

Reputation: 771

try this :

public void appendLog(String text)
{       
 File logFile = new File("sdcard/log.file");
 if (!logFile.exists())
 {
   try
  {
     logFile.createNewFile();
  } 
  catch (IOException e)
  {
     // TODO Auto-generated catch block
     e.printStackTrace();
  }
}
try
{
  //BufferedWriter for performance, true to set append to file flag
  BufferedWriter buf = new BufferedWriter(new FileWriter(logFile, true)); 
  buf.append(text);
  buf.newLine();
  buf.close();
}
catch (IOException e)
{
  // TODO Auto-generated catch block
  e.printStackTrace();
}
}

Upvotes: 1

Related Questions