我的程序在设备中崩溃。我想在我的设备上运行时准确地捕获我的程序的日志。即我想将日志写入我的 SD 卡,直到崩溃。我怎样才能做到这一点?
问问题
6911 次
4 回答
8
尝试这个
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);
}
}
于 2014-04-02T05:20:29.477 回答
4
一旦我从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();
}
}
这会连续打印日志,直到应用退出。
于 2014-04-02T05:12:02.293 回答
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);
}
}
于 2014-04-02T05:11:21.483 回答
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");
您可以非常轻松地使用内部和外部存储上的这个小库文件创建/读取/更新/删除。并且,考虑到写入同一个文件会增加它所占用的空间。您可以使用getSize()
同一个库进行验证。
于 2014-04-02T05:46:26.073 回答