将项目从 log4j 迁移到 slf4j+log4j

发布于 2024-10-21 13:59:49 字数 499 浏览 1 评论 0原文

我有一个直接使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

携余温的黄昏 2024-10-28 13:59:49

我认为切换的唯一好处是您可以仅通过一个框架汇集所有日志记录框架,这可能会简化您的配置。

我转向 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.

聚集的泪 2024-10-28 13:59:49

对我来说,除了你已经提到的那些功能之外,还有四个“杀手级”功能值得转移到 Logback(我个人切换了我当前的主要项目,工作完美):

  • 自动重新加载配置文件. 这对于生产系统来说非常棒。如果您只想将“调试”设置为一个类,但不想关闭整个系统,那么这个是无价的。
  • 能够在配置中使用包含文件。这使您可以为所有服务/JVM 拥有一组“主”日志记录设置,然后您可以指定可能需要特殊处理的任何包。目前我们有大约 10 个服务,它们都有一个很大但大部分相似的 log4j.xml 副本。我们现在将其更改为一个主 logback 配置文件,并将该文件包含在每个服务的 logback 配置文件中。主文件仍然很大,但特定于服务的文件应该非常小。更易于维护。
  • 条件处理。通过这种方式,您可以指定“if QAServer =>则将日志记录级别设置为“DEBUG”,否则为“INFO”之类的内容。同样,非常适合拥有不具有以下功能的单个配置:在生产和开发/QA 服务器之间进行更改。
  • 自动压缩和删除旧日志文件。您可以指定要保留的存档文件数量,并可选择在创建 n+1 文件后压缩它们。 ,它会检测到太多,并会删除最旧的一个,每个文件/服务均可配置,无需执行任何特定于系统的操作(批处理文件和/或脚本)

。 ,切换回 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):

  • Automatic reloading of config files. This is awesome for production systems. If you just want to set "debug" to one class, but not bring down the whole system, this one is priceless.
  • Ability to use include files in the config. This allows you to have a "master" set of logging settings for all your services/JVMs, and then you can specify any packages which might require special processing. We have about ~10 services currently, and they have all a large, but mostly similar, copy of log4j.xml. We're now changing it to one master logback config file, and have that file included in every service's logback config file. The master file will still be large, but the service-specific ones should be very small. Much more maintainable.
  • Conditional processing. This way you can specify things like "if QAServer => then set logging levels to "DEBUG", else "INFO". Again, great for having a single config that doesn't have to change between production and dev/QA servers.
  • Automatic compression and deletion of old log files. You can specify how many archived files you want to keep, and optionally compress them. After creating the n+1 file, it will detect that there's one too many, and will delete the oldest one. Again, configurable per file/service, no need to do anything system-specific (batch files and/or scripts).

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文