Search code examples
tomcatmavenpackagingslf4jlogback

How to package webapp when using slf4j/logback?


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?


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.