在与Android的日志记录器 [英] Logging with Logger under Android
问题描述
我已经注意到,在PC上,但不是在Android模拟器。
I've noticed that the following works on PC but not inside the Android simulator.
Logger logger = Logger.getLogger("foo");
logger.log(Level.INFO, "Number is: {0}", new Object[]{new Integer(42)});
这只是打印
Number is: {0}
为什么这个失败为Android?
Why does this fail for android?
推荐答案
我发现了一种迂回的方式解决。最后,我认为是在其中渲染只是消息,忽略PARAMS默认的Android日志实现一个格式化的地方。您应该能够使用日志API来安装你自己设计的格式。
I found a solution in a roundabout way. Ultimately, I think there is a Formatter somewhere in the default android logging implementation which is rendering just the message, ignoring the params. You should be able to use the Logging API to install a formatter of your own design.
我已经安装了新的处理程序为一个完全独立的原因。我使用的源代码可以在这里找到: http://4thline.org/projects/download/misc/一>
I had already installed a new Handler for a totally separate reason. The source I used is available here: http://4thline.org/projects/download/misc/
的处理程序是在 teleal常见-1.0.14-source.tar.gz 的存档。按照路径的src \\主\\ java的\\组织\\ fourthline \\机器人\\ UTIL \\ FixedAndroidHandler.java
The handler is in the teleal-common-1.0.14-source.tar.gz archive. Follow the path to src\main\java\org\fourthline\android\util\FixedAndroidHandler.java.
这个网站还提供code安装这个处理程序,但它是在一个不同的归档:绦-1.0-SNAPSHOT.tar.gz 的。找到 1.0的\\ src \\主\\ java的\\组织\\ teleal \\ COMMON \\日志\\ LoggingUtil.java
This site also provides code to install this handler, but it's in a different archive: sash-1.0-SNAPSHOT.tar.gz. Locate 1.0\src\main\java\org\teleal\common\logging\LoggingUtil.java.
您可以通过在应用程序启动时code在进行此调用的地方安装该处理程序:
You can install this handler by making this call somewhere in your app startup code:
LoggingUtil.resetRootHandler(new FixedAndroidHandler());
我发现的是,该Handler的格式化被嵌入的处理器内部的匿名类。如何方便。我可以看到格式化没有处理通过的LogRecord传递的参数。我只是说一个其他-if条件:
What I found was that the Formatter for this Handler is embedded as an anonymous class inside the Handler. How convenient. I could see that the Formatter did not process the parameters passed in via the LogRecord. I just added an "else-if" condition:
private static final Formatter THE_FORMATTER = new Formatter() {
@Override
public String format(LogRecord r) {
String msg = r.getMessage();
Object[] params = r.getParameters();
Throwable thrown = r.getThrown();
if (thrown != null) {
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
sw.write(r.getMessage());
sw.write("\n");
thrown.printStackTrace(pw);
pw.flush();
return sw.toString();
} else if ((params != null) && (params.length > 0) && (msg.indexOf("{0") >= 0)) {
return MessageFormat.format(msg, params);
} else {
return r.getMessage();
}
}
};
if测试是与其他日志格式code我见过一致。在理论上应该能够采取安装一个类似格式化到现有处理程序的更直接的方法。我还没有这个自己尝试由于这样的事实,上述解决方案是为我工作。
The if test is consistent with other log formatting code I've seen. In theory you should be able to take a more direct approach of installing a similar Formatter to an existing Handler. I haven't tried this myself due to the fact that the above solution is working for me.
这篇关于在与Android的日志记录器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!