Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 874
David Avatar asked Nov 23 '10 15:11

David


People also ask

Does SLF4J use Logback?

Native implementation of slf4j is logback, thus using both as logger framework implies zero memory and computational overhead.

Does spring boot uses SLF4J along with Logback implementation to log messages?

By default, Spring Boot includes SLF4J along with Logback implementations. If Logback is available, Spring Boot will choose it as the logging handler. You can easily configure logging levels within the application. properties file without having to create logging provider-specific configuration files such as logback.

Can we use SLF4J without log4j?

Suppose, if a project is already using log4j, and you included a library say Apache Active MQ, which has dependency on logback, another logging library, then you need to include them as well, but if Apache Active MQ uses SL4J, you can continue with your logging library, without pain of adding and maintaining new ...

What is JCL over SLF4J?

JCL-over-SLF4J, i.e. jcl-over-slf4j. jar, comes in handy in situations where JCL needs to be supported for backward compatibility reasons. It can be used to fix problems associated with JCL, without necessarily adopting the SLF4J API, a decision which can be deferred to a later time.


1 Answers

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.

like image 162
Aaron Digulla Avatar answered Sep 23 '22 06:09

Aaron Digulla