Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

migrating a project from log4j to slf4j+log4j

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?

like image 681
Yoni Avatar asked Mar 11 '11 10:03

Yoni


People also ask

Can SLF4J replace log4j?

Conclusion. So essentially, SLF4J does not replace Log4j, Both work together. SLF4j removes the tight coupling between the application and logging frameworks. It makes it easy to replace with any other logging framework in the future with a more capable library.

Can we use log4j and SLF4J together?

So essentially, SLF4J does not replace log4j; they both work together. It removes the dependency on log4j from your application and makes it easy to replace it in the future with the more capable library.

Is SLF4J better than log4j?

Unlike log4j, SLF4J (Simple Logging Facade for Java) is not an implementation of logging framework, it is an abstraction for all those logging frameworks in Java similar to log4J. Therefore, you cannot compare both. However, it is always difficult to prefer one between the two.


2 Answers

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.

like image 103
Augusto Avatar answered Oct 06 '22 00:10

Augusto


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.

like image 37
mjuarez Avatar answered Oct 05 '22 23:10

mjuarez