【发布时间】:2012-09-19 08:37:04
【问题描述】:
我知道这个话题已经谈了很多,但不是这个意思。 我需要将日志存储在 .txt 文件中,但我不能使用 log4j 或除 android.util.log 之外的任何其他类 我有这个解决方案,但它不是最好的。 对于具有与以下相同的信息:Log.i(TAG, "An INFO Message"); 我得写……
ERROR = logLevel < 3;
WARNING = logLevel < 2;
INFO = logLevel < 1;
if (INFO){
appendLog("LEVEL: I TIME: "+java.util.GregorianCalendar.DAY_OF_MONTH +
"-"+ java.util.GregorianCalendar.MONTH +" "+GregorianCalendar.HOUR_OF_DAY +":"+GregorianCalendar.MINUTE +
":"+GregorianCalendar.SECOND +"."+GregorianCalendar.MILLISECOND + " PID: "+
android.os.Process.myPid()+ " TID: "+android.os.Process.myTid()+ " Application: com.example.myapplication"+
" TAG:" +TAG+ " TEXT: An INFO Message");
}
然后……
public void appendLog(String text) {
File logFile = new File("sdcard/log.txt");
if (!logFile.exists()) {
try {
logFile.createNewFile();
}catch (IOException e){
e.printStackTrace();
}
}
try {
BufferedWriter buf = new BufferedWriter(new FileWriter(logFile, true));
buf.append(text);
buf.newLine();
buf.close();
} catch (IOException e) {
e.printStackTrace();
}
}
还有比这更优雅的解决方案吗?谢谢你帮助我。
【问题讨论】:
-
感谢 Rajesh,它非常接近我想要的。