Question

I have lots of web applications to build/package, and I would like to share their common libraries.

Regarding logging, I would like to bundle slf4j-api with each application, but consider the implementation as provided by my container (which is tomcat currently)

To do so, I copied the two jars, logback-classic and logback-core into $CATALINA\lib directory.

Unfortunately, at runtime, the binding between slf4j and its implementation fails with the following error message:

SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.

The only way I managed to make it work is when bundling the jar in the war files.

Any ideas?

Was it helpful?

Solution

This will not work as expected. Logging frameworks use a lot of static variables (a.k.a. global variables).

So every time you load a logging config, it will replace it for all applications deployed to your container. This is usually not what you want.

Bundle the log implementation with your WAR so the web container can make sure each web app gets its own set of global variables.

[EDIT] If you really want to control all apps with the same log config, you must move all log classes into the container. This includes slf4j.

I strongly advise to put the log config into a JAR and put it there as well. Or deploy a single dummy web app which just contains as few code as possible (so Tomcat will load it) plus the logback.xml

Otherwise, the start order of applications will determine what the logging will be when you get to a point where the log config has to change.

OTHER TIPS

did you respect

Placing one (and only one) of slf4j-nop.jar, slf4j-simple.jar, slf4j-log4j12.jar, slf4j-jdk14.jar or logback-classic.jar on the class path should solve the problem.

?

if you use e.g. hibernate it might introduce slf4j-log4j12.jar in your classpath, so there might be more than one slf4j implementation in your classpath

for me it works with slf4j and log4j

We put all jar for Slf4j & Logback into shared/lib of Tomcat. Then, we put a global logback.xml into shared/classes.

But i still don't know if it is a good practice. We have to manage a log by user, so we set MDC in order to use SiftingAppender. I have a question which is waiting for an answer on this subject :(

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top