我的程序在设备中崩溃了。我希望在我的设备中运行时准确捕获程序的日志。我想将日志写入我的SD卡,直至崩溃。我怎样才能做到这一点?
答案 0 :(得分:7)
试试这个
Thread.setDefaultUncaughtExceptionHandler(new ExceptionHandler(this)); // add this to your activity page
public class ExceptionHandler implements
java.lang.Thread.UncaughtExceptionHandler {
private final Context myContext;
private final String LINE_SEPARATOR = "\n";
UncaughtExceptionHandler defaultUEH;
public ExceptionHandler(Context con) {
myContext = con;
defaultUEH = Thread.getDefaultUncaughtExceptionHandler();
}
@SuppressWarnings("deprecation")
public void uncaughtException(Thread thread, Throwable exception) {
StringWriter stackTrace = new StringWriter();
exception.printStackTrace(new PrintWriter(stackTrace));
StringBuilder errorReport = new StringBuilder();
errorReport.append("************ CAUSE OF ERROR ************\n\n");
errorReport.append(stackTrace.toString());
errorReport.append("\n************ DEVICE INFORMATION ***********\n");
errorReport.append("Brand: ");
errorReport.append(Build.BRAND);
errorReport.append(LINE_SEPARATOR);
errorReport.append("Device: ");
errorReport.append(Build.DEVICE);
errorReport.append(LINE_SEPARATOR);
errorReport.append("Model: ");
errorReport.append(Build.MODEL);
errorReport.append(LINE_SEPARATOR);
File root = android.os.Environment.getExternalStorageDirectory();
String currentDateTimeString = DateFormat.getDateTimeInstance().format(
new Date());
File dir = new File(root.getAbsolutePath() + "/dir_name/log");
if (!dir.exists()) {
dir.mkdirs();
}
File file = new File(dir, "log.txt");
try {
BufferedWriter buf = new BufferedWriter(new FileWriter(file, true));
buf.append(currentDateTimeString + ":" + errorReport.toString());
buf.newLine();
buf.close();
} catch (IOException e) {
e.printStackTrace();
}
defaultUEH.uncaughtException(thread, exception);
System.exit(0);
}
}
答案 1 :(得分:2)
一旦我从SO的某处得到了这个。试试这个:
public static void printLog(Context context){
String filename = context.getExternalFilesDir(null).getPath() + File.separator + "my_app.log";
String command = "logcat -f "+ filename + " -v time *:V";
Log.d(TAG, "command: " + command);
try{
Runtime.getRuntime().exec(command);
}
catch(IOException e){
e.printStackTrace();
}
}
这将持续打印日志,直到应用程序退出。
答案 2 :(得分:1)
试试这个:
public void appendLog(String text) {
File logFile = new File("sdcard/log.file");
if (!logFile.exists()) {
try {
logFile.createNewFile();
}
catch (IOException e) {
Log.e(TAG, e.getMessage(), e);
}
}
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) {
Log.e(TAG, e.getMessage(), e);
}
}
答案 3 :(得分:0)
如果您只想捕获堆栈跟踪的异常,这在大多数情况下足以解决错误,那么 ACRA 是一个成功的解决方案。
如果您确实想要在SD卡上写一些内容,请考虑到您不能认为任何设备都有可以写入的外部存储设备。除非你验证这一点。
要提供另一种写入外部存储的选项,您可以使用这个简单的库:
的 android-simple-storage 强>
这是您使用它的方式:
Storage storage = SimpleStorage.getExternalStorage();
// create your own directory
storage.createDirectory("MyDir");
// create the file you want to write to inside your new directory
storage.createFile("MyDir", "MyFile.txt", "");
storage.appendFile("MyDir", "MyFile.txt", "your log line");
您可以非常轻松地在内部和外部存储上使用此tiny library文件创建/读取/更新/删除。并且,考虑到写入同一文件将增加所需的空间。您可以使用同一个库中的getSize()
进行验证。