Java Android 中的 LOG4J
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21307968/
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 in Android
提问by Midson
I have Java Project with lot of files, which is using LOG4J. Now I am trying to Port it to Android platform. Is it possible to reuse the code as-it is, with LOG4J function calls?
我有很多文件的 Java 项目,它使用 LOG4J。现在我正在尝试将其移植到 Android 平台。是否可以通过 LOG4J 函数调用按原样重用代码?
Current understanding:
目前的理解:
- Property configuration won't work (beans dependency)
- I tried with LOG4J for Android and SL4J Lib. No success.
- 属性配置不起作用(bean 依赖)
- 我尝试使用 LOG4J for Android 和 SL4J Lib。没有成功。
Working. But no use
在职的。但是没有用
org.apache.log4j.Logger root = org.apache.log4j.Logger.getRootLogger();
final SocketAppender appender = new SocketAppender("192.168.123.123", 3333);
root.addAppender(appender);
// SLF4J - Not working
org.slf4j.Logger logger;
logger = LoggerFactory.getLogger(MyClass.class);
// LOG4J for Android - Not working
ConfigureLog4J.configure();
logger = Logger.getLogger( MyClass.class );
Am I missing something? Pointer to any working examples?
我错过了什么吗?指向任何工作示例的指针?
采纳答案by Midson
Solved by using the android-logging-log4j.jar.
通过使用android-logging-log4j.jar 解决。
Here is sample code:
这是示例代码:
public class ALogger {
public static org.apache.log4j.Logger getLogger(Class clazz) {
final LogConfigurator logConfigurator = new LogConfigurator();
logConfigurator.setFileName(Environment.getExternalStorageDirectory().toString() + File.separator + "log/file.log");
logConfigurator.setRootLevel(Level.ALL);
logConfigurator.setLevel("org.apache", Level.ALL);
logConfigurator.setUseFileAppender(true);
logConfigurator.setFilePattern("%d %-5p [%c{2}]-[%L] %m%n");
logConfigurator.setMaxFileSize(1024 * 1024 * 5);
logConfigurator.setImmediateFlush(true);
logConfigurator.configure();
Logger log = Logger.getLogger(clazz);
return log;
}
}
In your code, replace the below lines:
在您的代码中,替换以下几行:
PropertyConfigurator.configure(MY_PROP_FILE);
logger = Logger.getLogger( MyClaZZ.class );
With:
和:
logger = ALogger.getLogger(MyClazz.class);
回答by KarolDepka
My answer, that presents a more future-proof and lateral-thinking approach ;-).
我的回答是一种更面向未来和横向思考的方法;-)。
I was using android-logging-log4j myself for some time, but it was causing massive warnings (which actually looked like errors) while dexing.
我自己使用 android-logging-log4j 有一段时间了,但它在 dexing 时导致了大量警告(实际上看起来像错误)。
The log4j jar also had a lot of unnecessary stuff in it, like the Chainsaw logging UI (written in Swing, thus useless on Android).
log4j jar 中还有很多不必要的东西,比如 Chainsaw 日志 UI(用 Swing 编写,因此在 Android 上没用)。
I've switched to logbackvia https://github.com/tony19/logback-android. So far I'm happy. No more massive spam while building.
我已经通过https://github.com/tony19/logback-android切换到logback。到目前为止,我很高兴。构建时不再有大量垃圾邮件。
Plus, looks like log4j 1.x is not developed anymore, look at https://github.com/apache/log4j/tree/trunk- last commit many years ago.
另外,看起来 log4j 1.x 不再开发,请查看https://github.com/apache/log4j/tree/trunk- 多年前最后一次提交。
And logback seems to be the successor.
而 logback 似乎是继任者。