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?
Native implementation of slf4j is logback, thus using both as logger framework implies zero memory and computational overhead.
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.
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 ...
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With