目前,我正在为我工作的公司开发应用程序,很多同事都在使用我的应用程序,但事实是,有时会发生崩溃/错误,并且我无法检查日志消息,所以我会希望将日志消息保存在文本文件中,以便在发生错误时可以检查日志文件,并更好地了解引起问题的原因。
我不需要通过电子邮件将日志发送给我,这足以将文件放在电话本地,因此我可以将其插入我的PC并提取日志文件。
目前,这是我管理日志的方式:
try {
//stuff...
}
catch (Exception ex) {
Log.e("Error", ex.getMessage());
}
这Log.e("Error", ex.getMessage());
是我要保存的。
尝试这个 :
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();
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句