登录到 Android 上的文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2116260/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Logging to a file on Android
提问by Greg B
Is there any way of retrieving log messages from an Android handset.
有没有办法从 Android 手机中检索日志消息。
I'm building an application which uses the GPS of my HTC Hero. I can run and debug the application from eclipse but this isn't a good use case of GPS, sat at my desk.
我正在构建一个使用我的 HTC Hero 的 GPS 的应用程序。我可以从 eclipse 运行和调试应用程序,但这不是 GPS 的一个很好的用例,坐在我的桌子上。
When I fire the app up when I am walking around, I get an intermittent exception. Is there anyway I can output these exceptions to a text file on the SD card or output calls to Log.x("")
to a text file so that I can see what the exception is.
当我四处走动时启动应用程序时,出现间歇性异常。无论如何我可以将这些异常输出到 SD 卡上的文本文件或输出Log.x("")
对文本文件的调用,以便我可以看到异常是什么。
Thanks
谢谢
EDIT : Solution
编辑:解决方案
Here is the code I finally went with...
这是我最终使用的代码...
Thread.currentThread().setUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
@Override
public void uncaughtException(Thread thread, Throwable ex) {
PrintWriter pw;
try {
pw = new PrintWriter(
new FileWriter(Environment.getExternalStorageDirectory()+"/rt.log", true));
ex.printStackTrace(pw);
pw.flush();
pw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
});
I had to wrap the line
我不得不换行
pw = new PrintWriter(new FileWriter(Environment.getExternalStorageDirectory()+"/rt.log", true));
in a try/catch as Eclipse would not let me compile the app. It kept saying
在 try/catch 中,因为 Eclipse 不允许我编译应用程序。一直说
Unhandled exception type IOException 1 quick fix Sorround with try/catch
Unhandled exception type IOException 1 quick fix Sorround with try/catch
So I did and it all works which is fine by me but it does make me wonder what Eclipse was on about...
所以我做了,一切正常,这对我来说很好,但它确实让我想知道 Eclipse 在做什么......
采纳答案by Dave Webb
You could use Thread.setUncaughtExceptionHandler()
to catch the Exceptions.
您可以Thread.setUncaughtExceptionHandler()
用来捕获异常。
Writing to SD Card is as simple as retrieving the directory for the card using Environment.getExternalStorageDirectory()
and creating a file there.
写入 SD 卡就像使用检索卡的目录并在Environment.getExternalStorageDirectory()
那里创建文件一样简单。
File f = new File(Environment.getExternalStorageDirectory(),filename);
You will need to give you app the correct permission to write to the SD Card by adding this to your Manifest:
您需要通过将其添加到您的清单中来为您的应用程序提供正确的权限来写入 SD 卡:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
回答by Rolf Kulemann
Log4j or slf4j can also be used as logging frameworks in Android together with logcat. See the project android-logging-log4j. Configuring logging to a (rotating) file(s) is very easy.
Log4j 或 slf4j 也可以与 logcat 一起用作 Android 中的日志框架。请参阅项目android-logging-log4j。配置日志记录到(旋转)文件非常容易。
static {
final LogConfigurator logConfigurator = new LogConfigurator();
logConfigurator.setFileName(Environment.getExternalStorageDirectory() + "myapp.log");
logConfigurator.setRootLevel(Level.DEBUG);
// Set log level of a specific logger
logConfigurator.setLevel("org.apache", Level.ERROR);
logConfigurator.configure();
}
回答by darius
You could take a look at microlog4android. They have a solution ready to log to a file.
你可以看看 microlog4android。他们有一个解决方案可以记录到文件中。
回答by paral
I tried both options above (microlog4android & android-logging-log4j) and they were a struggle to try to get working. Once they compiled fine then I would get Runtime java.lang.NoClassDefFoundError.
我尝试了上面的两个选项(microlog4android 和 android-logging-log4j),但他们很难开始工作。一旦他们编译好了,我就会得到运行时 java.lang.NoClassDefFoundError。
So then I stumbled on logback-android... also found here http://logback.qos.ch/
然后我偶然发现了logback-android...也在这里找到了http://logback.qos.ch/
It has much better instructions and I got it working in a couple hours.
它有更好的说明,我在几个小时内就让它工作了。
This appears to be the best logging solution for Android.
这似乎是 Android 的最佳日志记录解决方案。
回答by klimat
Use slf4-androidlib created by BrightInventions. It's simple implementation of slf4j apiusing android java.util.logging.*.
使用BrightInventions创建的slf4 -android库。这是使用 android java.util.logging.*的slf4j api的简单实现。
Features:
特征:
- logging to file out of the box
- logging to any other destination by
LoggerConfiguration.configuration().addHandlerToLogger
- shake your device to send logs with screenshot via email
- really small, it tooks only ~55kB
- 开箱即用地登录到文件
- 登录到任何其他目的地
LoggerConfiguration.configuration().addHandlerToLogger
- 摇动您的设备以通过电子邮件发送带有屏幕截图的日志
- 真的很小,只需要~55kB
slf4android is maintained mainly by @miensol.
slf4android 主要由@miensol维护。
Read more about slf4android on our blog:
在我们的博客上阅读有关 slf4android 的更多信息:
回答by Mohamed Taher Alrefaie
Based on @Greg B solution and @JPM question in the comment, I made this static method in a seperate class and each time you instanciate a new thread, call it.
根据评论中的@Greg B 解决方案和@JPM 问题,我在一个单独的类中创建了这个静态方法,每次实例化一个新线程时,都调用它。
package com.stackoverflow.
包 com.stackoverflow。
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import android.os.Environment;
public class UnhandledExceptionHandler {
public static void setUndhandledException(Thread thread) {
if (thread.getUncaughtExceptionHandler() == null) {
Thread.currentThread().setUncaughtExceptionHandler(
new Thread.UncaughtExceptionHandler() {
@Override
public void uncaughtException(Thread thread,
Throwable ex) {
PrintWriter pw;
try {
pw = new PrintWriter(new FileWriter(Environment
.getExternalStorageDirectory()
+ "/rt-networkandgps"
+ "/gpx/"
+ thread.getId() + ".log", true));
ex.printStackTrace(pw);
pw.flush();
pw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
}
}
Call it from anythread like this,
像这样从任何线程调用它,
UnhandledExceptionHandler.setUndhandledException(Thread.currentThread());