如何将设备上的日志重定向到文件?
我的应用程序在模拟器上运行良好,但在设备上挂起 - 我希望能够查看设备上的日志,而无需使用sdk和其工具。
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);
}
}
Microlog是一种Java ME和Android的日志框架,受到Log4j的启发。
(c) http://en.wikipedia.org/wiki/Log4j
"在Android上使用Microlog进行日志记录并在模拟器中查看日志" by Johan Karlsson
我正在使用从安卓市场免费下载的应用程序aLogCat。
非常好用。
日志框架(如log4j)已经提供了像记录到文件这样的功能。使用{{link1:android-logging-log4j}},可以将日志记录到旋转的日志文件和logcat中。还可以参见{{link2:Android中的log4j支持}}。