使用哪个 Android 日志框架?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10514175/
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
Which Android logging framework to use?
提问by Marcell
My question seems to be easily answerable, but there are several good solutions. I like to choose the 'best' one.
我的问题似乎很容易回答,但有几个很好的解决方案。我喜欢选择“最好的”。
Available frameworks (feel free to suggest more):
可用的框架(请随意提出更多建议):
Pros/Cons:
优点缺点:
Androlog:
安洛洛格:
- Pro: Similar to Android logging framework, so there are only small changes in the existing code; Able to send crash reports with more detail in the error report (the logs around the exception); Nice logs
- Con: No standard java "getLogger" approach; Production configuration achievable by uploading property file to sdcard; I need to call init logging manually; Need to create LOG_TAG like constants, or hack it to create log tag constants by Aspect to achieve the standard behavior: tags are the class names; When logging is a business requirement, we need to test it. Testing static calls on Android nearly impossible; Logger can not be injected by framework
- 优点:类似于Android日志框架,所以对现有代码只有很小的改动;能够在错误报告(异常周围的日志)中发送包含更多详细信息的崩溃报告;不错的日志
- 缺点:没有标准的java“getLogger”方法;生产配置可通过上传属性文件到sdcard实现;我需要手动调用 init 日志记录;需要像常量一样创建LOG_TAG,或者hack它通过Aspect创建日志标签常量来实现标准行为:标签是类名;当日志记录是一项业务需求时,我们需要对其进行测试。在 Android 上测试静态调用几乎是不可能的;Logger 不能被框架注入
Log4J-Android:
Log4J-Android:
- Pro: Standard way to log in Java; Compatible with SLF4J; Able to parse property files;
- Con: No built-in crash report system; Seems to me: its not commonly used, so it might me dangerous to use it;
- 优点:Java 登录的标准方式;兼容SLF4J;能够解析属性文件;
- 缺点:没有内置崩溃报告系统;在我看来:它不常用,所以使用它可能很危险;
SLF4J-Android:
SLF4J-Android:
- Pro: Seems to be developed by more people like Log4J-Android; The
logger.debug("Some log message. Details: {}", someObject.toString());
is a good, and effective way to skip string concatenations if the logger is turned off; lightweight logger binding that delegates toandroid.util.Log
. - Con: Auto generated log tags that are <= 23 characters long due to a length restriction of log tags on the Android platform (e.g.,
com.example.myapp.MyClass
tag translated toc*.e*.m*.MyClass
), which can result in the same log tag for different classes (e.g.,com.example.app.MyClass
andcom.example.anotherapp.MyClass
both translate toc*.e*.a*.MyClass
); No built in crash reporting system.
- 亲:貌似Log4J-Android这样的人开发的比较多;该
logger.debug("Some log message. Details: {}", someObject.toString());
是一个很好的,并跳过字符串连接,如果记录器被关闭的有效途径; 委托给android.util.Log
. - 缺点:自动生成所<= 23个字符长由于Android平台上的日志标签的长度的限制(例如,日志标签
com.example.myapp.MyClass
标签翻译成c*.e*.m*.MyClass
),其可以导致对于不同类别(例如,相同的日志标签,com.example.app.MyClass
以及com.example.anotherapp.MyClass
同时平移到c*.e*.a*.MyClass
); 没有内置的崩溃报告系统。
Besides of these, I like Androlog behavior, but I'm a Java dev, familiar with log4j/slf4j. We will definitely need crash report system, but there are several frameworks for crash reporting (beside of android default crash report).
除此之外,我喜欢 Androlog 行为,但我是 Java 开发人员,熟悉 log4j/slf4j。我们肯定需要崩溃报告系统,但是有几个崩溃报告框架(除了 android 默认崩溃报告)。
I can combine some of them, for example use Log4J android, but create an appender to use the androlog framework, but sooner or later it will be a mess, which should be avoided.
我可以结合其中一些,例如使用Log4J android,但是创建一个appender来使用androlog框架,但迟早会变得一团糟,应该避免。
Thanks for your suggestions, I hope the results will help decide others in the future.
感谢您的建议,我希望结果有助于将来决定其他人。
Edit: As mentioned below, I can combine for ex: log4j-android with slf4j (whitch I prefer to do if I'll use log4j, because the log formatting support ("{}",...) ), but it does not answers the question. I have to choose a framework, then I can decorate it with the SLF4J facade.
编辑:如下所述,我可以将例如 log4j-android 与 slf4j 结合使用(如果我使用 log4j,我更喜欢这样做,因为日志格式支持 ("{}",...) ),但它确实如此不回答问题。我必须选择一个框架,然后我可以用 SLF4J 门面装饰它。
回答by stefan.nsk
The better way. I think, is to use SLF4J API + some of its implementation.
更好的办法。我想,就是使用SLF4J API + 它的一些实现。
For Android applications you can use the following:
对于 Android 应用程序,您可以使用以下内容:
- Android Loggeris the lightweight but easy-to-configure SLF4J implementation (< 50 Kb).
- LOGBackis the most powerful and optimized implementation but its size is about 1 Mb.
- Any other by your taste: slf4jandroid, slf4j-android.
- Android Logger是轻量级但易于配置的 SLF4J 实现(< 50 Kb)。
- LOGBack是最强大和优化的实现,但它的大小约为 1 Mb。
- 其他任何你喜欢的:slf4jandroid,slf4j-android。
回答by silwar
Please check thisfirst answer
请检查这个第一个答案
It says:
它说:
SLF4J is basically an abstraction layer. It is not a logging implementation. It means that if you're writing a library and you use SLF4J, you can give that library to someone else to use and they can choose which logging implementation to use with SLF4J e.g. log4j or the Java logging API. It helps prevent projects from being dependent on lots of logging APIs just because they use libraries that are dependent on them.
So, to summarise: SLF4J does not replace log4j, they work together. It removes the dependency on log4j from your library/app.
SLF4J 基本上是一个抽象层。它不是日志记录实现。这意味着如果您正在编写一个库并使用 SLF4J,您可以将该库提供给其他人使用,他们可以选择与 SLF4J 一起使用的日志记录实现,例如 log4j 或 Java 日志记录 API。它有助于防止项目仅仅因为使用依赖于它们的库而依赖于大量日志记录 API。
所以,总结一下:SLF4J 不会取代 log4j,它们一起工作。它从您的库/应用程序中删除了对 log4j 的依赖。
回答by k3b
I have tried original slf4j.org-androidbut unfortunately this jar was not able to get debug/verbose messages to be logged because it internally uses LOG.isDebugEnabled() for debug output wich seems always to be false.
我尝试过原始的 slf4j.org-android但不幸的是,这个 jar 无法获取要记录的调试/详细消息,因为它在内部使用 LOG.isDebugEnabled() 进行调试输出,这似乎总是错误的。
currently i use the alternative lp0-slf4j-androidimplementation that uses a properties-file with the logging settings where i can also get debug/verbose messages if enabled.
目前我使用替代lp0-slf4j-android实现,它使用带有日志记录设置的属性文件,如果启用,我还可以获得调试/详细消息。