Android 中的 log4j 支持
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2915150/
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
log4j support in Android
提问by Travis
I am attempting to shoehorn an existing SDK onto an android device and one of the dependencies of said SDK is Apache log4j. I am able to load my test program onto the android emulator but when the log4j object "PropertySetter" is called the program fails with a verification exception. Is there a way to ameliorate this issue?
我试图将现有的 SDK 硬塞到 android 设备上,并且该 SDK 的依赖项之一是 Apache log4j。我能够将我的测试程序加载到 android 模拟器上,但是当调用 log4j 对象“PropertySetter”时,程序失败并出现验证异常。有没有办法改善这个问题?
回答by Eran
Actually using slf4j turned out a remarkably painless process for me, and it seems the common case, at least for libraries that use straightforward log4j features. You don't really need to swap slf4j in for log4j, only add two slf4j libraries to your project from http://www.slf4j.org/download.html:
实际上,使用 slf4j 对我来说是一个非常轻松的过程,这似乎是常见的情况,至少对于使用简单 log4j 功能的库而言是这样。您真的不需要将 slf4j 换成 log4j,只需从http://www.slf4j.org/download.html向您的项目添加两个 slf4j 库:
-- the slf4j library for Android (currently slf4j-android-1.6.1-RC1.jar)
-- the log4j over slf4j (http://www.slf4j.org/legacy.html#log4j-over-slf4j) bridge.
-- 适用于 Android 的 slf4j 库(当前为 slf4j-android-1.6.1-RC1.jar)
-- slf4j 上的 log4j(http://www.slf4j.org/legacy.html#log4j-over-slf4j)桥接。
The latter defines the core log4j classes used by typical implementations and bind them to the slf4j Android implementation. Once the libraries are added the code works.
后者定义了典型实现使用的核心 log4j 类,并将它们绑定到 slf4j Android 实现。添加库后,代码即可运行。
回答by sean
I successfully got log4j working on android with a Socket Appender and Log4j Chainsaw. All code is located in this repository. Slf4j set up and working too. Be aware you have to configure it programmatically. You cannot use .properties or .xml files the parser wont work on android. Enjoy.
我使用 Socket Appender 和 Log4j Chainsaw 成功地让 log4j 在 android 上工作。所有代码都位于此存储库中。Slf4j 也设置并运行。请注意,您必须以编程方式对其进行配置。您不能使用 .properties 或 .xml 文件,解析器无法在 android 上运行。享受。
回答by Rolf Kulemann
There is a new project, which enables log4j on android. Also using log4j over slf4j is possible. It also provides an appender for LogCat. See Logging in Android using Log4J.
有一个新项目,它在 android 上启用 log4j。也可以在 slf4j 上使用 log4j。它还为 LogCat 提供了一个 appender。请参阅使用 Log4J 在 Android 中登录。
The following example shows how to configure and use log4j in Android.
以下示例展示了如何在 Android 中配置和使用 log4j。
Configure the log4j system in Android
在Android中配置log4j系统
import org.apache.log4j.Level;
import android.os.Environment;
import de.mindpipe.android.logging.log4j.LogConfigurator;
/**
* Simply place a class like this in your Android applications classpath.
*/
public class ConfigureLog4J {
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();
}
}
Logging in Android using log4j using slf4j API
使用 slf4j API 使用 log4j 登录 Android
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class ExampleLog4JOverSLF4J {
private final Logger log = LoggerFactory.getLogger(ExampleLog4JOverSLF4J.class);
public void myMethod() {
log.info("This message should be seen in log file and logcat");
}
}
Logging in Android using log4j API
使用 log4j API 登录 Android
import org.apache.log4j.Logger;
public class ExampleLog4J {
private final Logger log = Logger.getLogger(LogConfiguratorTest.class);
public void myMethod() {
log.info("This message should be seen in log file and logcat");
}
}
回答by sean wagner
Check out this project for a complete implementation: http://code.google.com/p/log4j-android/
查看此项目以获得完整的实现:http: //code.google.com/p/log4j-android/
回答by sean wagner
The parser for log4j configuration files is not android safe.slf4j's android compatibility thing with log4j just overrides the log4j classes you will use and forces them to use android logcat style logging. You still don't get the full flexibility of log4j on android. I ported log4j on android in my project https://sourceforge.net/projects/log4j-android/all you have to do is add the two jars in the binaries directory to you classpath. Then
log4j 配置文件的解析器不是 android 安全的。slf4j 与 log4j 的 android 兼容性只是覆盖了您将使用的 log4j 类并强制它们使用 android logcat 样式日志记录。您仍然无法在 android 上获得 log4j 的全部灵活性。我在我的项目https://sourceforge.net/projects/log4j-android/ 中移植了 android 上的 log4j,您所要做的就是将二进制目录中的两个 jar 添加到您的类路径中。然后
static {
org.apache.log4j.Logger root = org.apache.log4j.Logger.getRootLogger();
final SocketAppender appender = new SocketAppender("192.168.1.4", 4445);
root.addAppender(appender);
}
private static final org.slf4j.Logger logger = LoggerFactory.getLogger(MyClass.class);
static {
logger.info("Hello logger");
}
This will start sending out messages to the remote host you specified. You can then see this messages with Chainsaw http://logging.apache.org/log4j/docs/webstart/chainsaw/chainsawWebStart.jnlp. To make chainsaw work click the second check box on the dialog that pops up hit ok, start your app and a new tab should appear. Be aware your firewall might block it...
这将开始向您指定的远程主机发送消息。然后,您可以使用 Chainsaw http://logging.apache.org/log4j/docs/webstart/chainsaw/chainsawWebStart.jnlp查看此消息。要使链锯工作,请单击弹出的对话框中的第二个复选框点击确定,启动您的应用程序,并应出现一个新选项卡。请注意您的防火墙可能会阻止它...
回答by user413457
I would recommend trying to swap in slf4j in place of log4j. It's not a painless switch but its likely to be easier than what you have. slf4j provides a common front-end for several loggers including log4j and there is an slf4j-android package.
我建议尝试用 slf4j 代替 log4j。这不是一个无痛的切换,但它可能比你拥有的更容易。slf4j 为包括 log4j 在内的多个记录器提供了一个通用前端,并且有一个 slf4j-android 包。
No, Android's logging mechanism is not decent. It's very inadequate compared to what log4j can do for you.
不,Android 的日志记录机制不太好。与 log4j 可以为您做的相比,这是非常不够的。