将项目从 log4j 迁移到 slf4j+log4j
我有一个直接使用 log4j 的大型 Web 项目,以及许多第 3 方库和日志记录库的组合。
- 我们的代码库 - 直接使用 log4j。
- Hibernate - 使用 slf4j 和 slf4j-log4j 绑定。
- Spring - 使用公共日志记录。因此,它使用 jcl-over-slf4j 桥接 API、slf4j 本身和 slf4j-log4j 绑定。
- 其他许多库,使用公共日志记录或 log4j。
我正在考虑将我们自己的代码库迁移到 slf4j api,但我不确定好处是否足够强大并且值得付出努力。目前我知道以下好处:
- 更干净的 api。
- 性能改进 - 即使用参数化日志记录方法的能力。
- 将来能够轻松切换到 logback(目前 logback 是不可能的)。
- 不需要额外的罐子,因为我已经有了它们。
还有其他好处吗?有什么我还没有意识到的缺点吗?
I have a large web project that uses log4j directly, together with many 3rd-party libraries and a mix of logging libraries.
- our code base - uses log4j directly.
- Hibernate - uses slf4j, and the slf4j-log4j binding.
- Spring - uses commons-loggings. Thus, it uses the jcl-over-slf4j bridge api, slf4j itself, and slf4j-log4j binding.
- Other numerous libraries, using either commons loggings or log4j.
I am considering migrating our own code base to slf4j api, but I am not sure if the benefits are strong enough and worth the effort. Currently I am aware of the following benefits:
- Cleaner api.
- Performance improvements - namely the ability to use parameterized logging methods.
- Ability to switch easily to logback in the future (currently logback is out of the question).
- No need for additional jars, since I already have them.
Are there any other benefits? Are there any drawbacks that I am not aware of yet?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为切换的唯一好处是您可以仅通过一个框架汇集所有日志记录框架,这可能会简化您的配置。
我转向 slf4j(这只适用于 slf4j + logback)的主要原因可能是您可以 通过 JMX 重新加载配置< /a>,当您遇到的问题随着服务器重新启动而消失时,这非常有用。
The only benefit I see for switching, is that you can funnel all the logging frameworks through only one framework, which might simplify your configuration.
Probably the main reasons why I moved to slf4j (this only applies to slf4j + logback) is that you can reload the configuration via JMX, which is GREAT when you have a problem that disappears with a server restart.
对我来说,除了你已经提到的那些功能之外,还有四个“杀手级”功能值得转移到 Logback(我个人切换了我当前的主要项目,工作完美):
。 ,切换回 log4j 非常容易,因为迁移到 Logback 需要使用 SLF4J(Log4j 也支持),因此来回切换只需要选择要删除的 jar 文件(Log4j 或 Logback 的)即可。 xml 或 logback 配置文件位于类路径中,日志记录将正常工作。
For me, there were four "killer" features which were worth the pain in moving over to Logback, on top of the ones you already mentioned (I personally switched my current major project, working flawlessly):
By the way, after doing this change, it's trivially easy to switch back to log4j. Since migrating to Logback required using SLF4J, which Log4j also supports, switching back and forth only requires you to choose which jar file to drop (Log4j's or Logback's). As long as the corresponding log4j.xml or logback config files are in the classpath, logging will work correctly.