登录设备到文件

时间:2010-02-16 13:19:25

标签: java android logging

如何将设备上的日志记录重定向到文件?

我的应用程序挂在设备上,但在模拟器上运行良好 - 我希望在没有sdk及其工具的情况下看到我的设备上的日志。

5 个答案:

答案 0 :(得分:5)

答案 1 :(得分:4)

我发现了Logcat的优秀功能。它可以使用简单的命令参数“-f”将输出重定向到文件本身。 要使用它,您可以在应用程序aLogcat中编写Logcat包装器。显然我做了这个:))

为了在android上使用logcat我编写了这段代码:

Process proc = null;
try {
  proc = Runtime.getRuntime().exec(new String[] { "logcat", <!parametes_here!> });
  mReader = new BufferedReader(new InputStreamReader(proc.getInputStream()), 1024);
  String line;
  while ((line = mReader.readLine()) != null) {
   if (line.length() == 0) {
    continue;
   }
  mHandler.sendMessage(mHandler.obtainMessage(Logcat.MSG_READ_LINE, line));
 }
} catch (IOException e) {
 Log.e(TAG, "Cannot start process", e);
} finally {
  if (mReader != null)
  try {
    mReader.close();
  } catch (IOException e) {
    Log.e(TAG, "Cannot close stream", e);
 }
}

答案 2 :(得分:3)

答案 3 :(得分:1)

我正在使用从Android市场免费下载的应用程序aLogCat。

效果非常好。

答案 4 :(得分:0)

log4j等日志框架已经提供了记录到文件等功能。使用android-logging-log4j可以登录到旋转日志文件和logcat。另请参阅log4j support in Android