本问题已经有最佳答案,请猛点这里访问。
我的程序在设备中崩溃。 我想在设备中运行时准确捕获程序的日志,即我要将日志写到sd卡中,直到崩溃为止。 我该如何实现?
您只需将设备连接到USB端口并运行应用程序,然后检查DDMS中的日志,为什么要将日志写入文件中?
我不知道我的外部设备发生了什么。 那只是我问如何将日志写入我的sdcard或其他东西。
我现在没有关于如何在设备中写入异常日志的信息,但是我已经实现了ARCA来跟踪崩溃。
尝试这个
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 ="
";
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 ************
");
errorReport.append(stackTrace.toString());
errorReport.append("
************ DEVICE INFORMATION ***********
");
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);
}
}
用代码给出一些解释。
但是万美元的问题,如何使用呢?
感谢杜德的工作
请指导一下! Thread.setDefaultUncaughtExceptionHandler(new ExceptionHandler(this));此shud在onCreate()或onResume()中调用。 我的意思是更好,我无法决定! 如果我们在onCreate()中调用它,则ExceptioHandler将被调用一次;而在onResume()中被调用,则每当Activity出现在前台时都将被调用。
一旦我从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();
}
}
这将连续打印日志,直到应用程序退出。
创建空的app_log文件..?
@strawberry立即尝试
@strawberry>。 您解决问题了吗?
是的,我使用Thread.setDefaultUncaughtExceptionHandler解决了这一问题。
@Strawberry然后您可以接受对您有帮助的答案...
尝试这个 :
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);
}
}
如果您只想使用stacktrace捕获异常,这在大多数情况下足以解决问题,那么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","");
随时在此文件后附加任何字符串(或byte []):
storage.appendFile("MyDir","MyFile.txt","your log line");
您可以使用内部和外部存储上的这个小型库文件轻松创建/读取/更新/删除。并且,要考虑到写入同一文件会增加其占用的空间。您可以使用同一库中的getSize()进行验证。