将 Log4J log.debug 语句迁移到 SLF4J 的参数化消息?

发布于 2024-11-23 22:00:46 字数 548 浏览 1 评论 0 原文

我有大约 400 个生产 Java 源代码文件,每个文件大约有一百到两万行代码,用于从 Log4J 字符串连接更改为 SLF4J 的参数化日志记录。

if(log.isDebugEnabled(){
    log.debug("Came here with value: " + car.getName());
}

我想利用 SLF4J 的参数化日志记录

log.debug("来到此处的值为:{}", car.getName());

我正在考虑编写一个脚本来自动化这个过程,或者实际上有没有办法做到这一点?

我想要更改为 SLF4J 的参数化日志记录的主要原因是性能

将使用 SLF4J + Log4J,因为 LogBack + SLF4J 需要 JavaSE 5 及更高版本,而我需要在 J2SE 1.4 JVM 环境上工作。

I have around 400 of production Java source code files with around hundred to twenty thousand lines of codes each to change out from Log4J String concatenation to SLF4J's parameterized logging.

if(log.isDebugEnabled(){
    log.debug("Came here with value: " + car.getName());
}

I would like to take advantage of SLF4J's logging parameterized

log.debug("Came here with value: {}", car.getName());

I was thinking of writing a script to automate this process, or actually is there a way way of doing this?

The main reason why i would like to change to SLF4J's parameterized logging is due to performance.

Will be using SLF4J + Log4J due to LogBack + SLF4J requires JavaSE 5 and above, while i need to work on a J2SE 1.4 JVM environment.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

凉城凉梦凉人心 2024-11-30 22:00:46

我怀疑使用 SLF4J/LOG4J 时是否会有任何性能改进,因为
org.slf4j.impl.Log4jLoggerAdapter将使用与您已经使用的几乎相同的代码(两次评估记录器是否启用)。它首先会检查是否启用了调试日志记录,然后将格式化的消息发送到 org.apache.log4j.Category 再次检查消息是否应记录在配置的级别中。

尽管如此,更改参数化日志记录还是有意义的,因为它更具可读性且更短。不幸的是,我不知道重构代码的任何授权方法。

编辑:刚刚注意到 SLF4J Migrator 的存在似乎有助于执行此操作一些基本的迁移(导入行和记录器声明)。这可能有助于开始迁移 - 即使日志记录语句不会被触及并且必须手动重构。

I doubt that you will have any perfomance improvements when using SLF4J/LOG4J, as the
org.slf4j.impl.Log4jLoggerAdapter will use nearly the same code as you already do (with twice evaluating whether the logger is enabled or not). It first will check if debug logging is enabled and than sends the formatted mesage to the org.apache.log4j.Category which again checks if the message should be logged in the configured level.

Neverthelsse it makes sence to change to the parameterized logging as it is far more readable and shorter. Unfortunately I do not know about any authomized way to refactor the code.

EDIT: Just noticed that a SLF4J Migrator exists which seems to help doing some basic migrations (import lines and logger declarations). That might help to start a migration - even though the logging statements won't be touched and have to be refactored manually.

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