开发者

Migrate Log4J log.debug statements to SLF4J's parameterized messages?

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开发者_如何学JAVA 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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜