Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use log4j 2.0 and slf4j and Commons Logging together

I currently am starting a new Webapp (running on tomcat 6) I have components using slf4j and components using commons logging I plan to use log4j 2.0 as log implementation due to several reasons (mainly for the appenders:SocketAppender and SyslogAppender but also because of the promoted config reloading without loss of log events)

Now my questions are: - To which interface do I program my new classes? loag4j or slf4j? or even commons logging?

  • What's the preferred way to deploy the jars? put them in my application war or do i put them into the tomcat libs?

  • what jars do I need to deploy? log4j (including slf4j and commons bindings), commons logging (slf4j-api-1.7.2.jar) and slf4j api (slf4j-api-1.7.2.jar)

like image 416
raudi Avatar asked Feb 26 '13 17:02

raudi


People also ask

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.

Does SLF4J use commons-logging?

The underlying logging framework can be any of the frameworks supported by SLF4J. Often times, replacing commons-logging. jar with jcl-over-slf4j. jar will immediately and permanently solve class loader issues related to commons logging.

Does commons-logging work with Log4j2?

Working with Log4j2 x, is also not compatible out of the box with commons-logging. For this reason, you need to add a JCL bridge to make log4j2 work. With these dependencies along with a log4j2. xml configuration, you could easily redirect all logs from apache commons into the Log4J system.

Is commons-logging affected by log4j?

commons-logging is more of abstraction. It can live without LOG4J also. If Log4j exists, it chooses it. Otherwise other default available.


1 Answers

To which interface do I program my new classes? loag4j or slf4j?

If you're going to use SLF4J, program to that interface. It offers the most flexibility for underlying logging implementation. The point of slf4j is to be an interface that you can program to, so in the future if you decide to switch to, say, logback, you won't have to rewrite your code.

What's the preferred way to deploy the jars? put them in my application war or do i put them into the tomcat libs?

Put them in your WAR.

The only reason (imo) to put JARs in the Tomcat libs directory is if they need to load a native library. Since Java won't allow you to load the same native library from two different classloaders, you need to put then in a common location. But that doesn't apply here.

Some people think of the lib directory as a way to save space. That may have been valid when "server-class" machines had 1 GB of RAM, but it isn't any more. And avoiding lib means you avoid most of the hard-to-debug classloading issues.

what jars do I need to deploy?

  • slf4j-api is the basic API
  • slf4j-log4j12 routes the actual logging to Log4J
  • jcl-over-slf4j intercepts Commons Logging and routes it through SLF4J (to Log4J)
  • log4j will be your physical logging framework

I'm assuming that you already have configuration for Log4J and/or are comfortable writing that configuration. If not, and all you care about is dealing with code that uses Log4J internally, there's log4j-over-slf4j, which will intercept Log4J calls. Then you need to pick a framework, such as Logback.

(note: I originally added links to all of these packages, but don't have enough rep to post them. So here's a single link to the Maven repository with all of the SLF4J packages highlighted)

like image 192
parsifal Avatar answered Oct 05 '22 09:10

parsifal