using slf4j to log in spring when I can't get rid of commons-logging - spring

I'm using some a component other than Spring that has a commons-logging dependency.
So, excluding commons-logging from the spring dependency doesn't get rid of it, and so the instructions here aren't helping. Is there some more declarative way to push Spring 3.0.5 to talk to slf4j?

I had an issue on Tomcat 6 where Commons Logging wouldn't play nice with SLF4J which got dragged in from Hibernate. We had configured Commons Logging to go to Log4J anyway. After explicitly including "slf4j-log4j 1.6.1" and its "jcl-over-slf4j 1.6.1" component to take out Commons Logging, I still got errors from Commons Logging. The answer lied in this blog post: You explicitly include Commons Logging at version "99.0-does-not-exist". This includes an empty JAR, so that only the SLF4J code gets executed now.

If you are using Maven, please read the FAQ entry about excluding commons-logging.

Use the SLF4j bridge

Related

Why does spring-boot-starter-logging require a dependency on log4-to-slf4j

I have a Spring Boot 2.6.3 project that uses spring-boot-starter. When I run the dependecy:tree goal in maven, I see that spring-boot-starter-logging depends on both logback and (indirectly) log4j. Why does spring-boot-starter-logging require a dependency on log4-to-slf4j?
Spring Boot has bindings for all major logging frameworks. With a single configuration you can concentrate logs sent through SLF4J, Log4j 2.x API or java.util.logging.
Therefore the spring-boot-starter-logging provides:
a binding for SLF4J (logback-classic),
an implementation of the Log4j 2.x API (log4j-to-slf4j). Remark that this is not the standard Log4j 2.x Core implementation.
a handler for java.util.logging (jul-to-slf4j).
Remark that spring-boot-starter-log4j2 does the same thing and redirects the frameworks above to Log4j 2.x Core.
The big absent in this picture is Jakarta Commons Logging, which is only able to bind to java.util.logging (hence not directly to neither Logback nor Log4j 2.x Core). However spring-core depends on spring-jcl, which binds JCL directly to SLF4J or the Log4j 2.x API and can entirely replace the original JCL.
A fifth API (Log4j 1.x) was supported in Spring Boot 1.x.

Logback support in Quarkus

I have tried to add logback in Quarkus, but later realised Quarkus does not support logback. If I am wrong, May I know how logback can be configured in Quarkus with an example. Thanks in advance.
No, in Quarkus, we standardized on using JBoss Logging for the logging.
And AFAIK we don't even have a bridge from logback to JBoss Logging as for some other logging components so you can't even use the Logback API in your application.
In currrent version 1.6.0.final of Quarkus You can use one of the Logging API's below
JDK java.util.logging
JBoss Logging
SLF4J
Apache Commons Logging
https://quarkus.io/guides/logging
There is a Quarkus extension which enables logback: https://quarkiverse.github.io/quarkiverse-docs/quarkus-logging-logback/dev/index.html
However there are some restrictions, mostly due to internal Quarkus specifics:
Your Quarkus version should match version used for 'quarkus-logging-logback' extension build
If you want to use own appenders/listeners in 'logback.xml' - you should put them in separate library and include them in your project through library

Application using jboss-logging when it should use log4j

I'm upgrading a Spring MVC application from Spring 3.1 to 4.3, and Hibernate 3.6 to 5.2. I'll be running this in Wildfly 8. Dependencies are managed by Maven.
Spring uses commons-logging, which looks at the classpath of the application and attempts to choose a suitable logging framework. In my case, it seems to be choosing the wrong one. I have included log4j in my pom.xml, and checking the dependency hierarchy I can see that jboss-logging is not there. Here is the error message I'm getting:
Invocation of init method failed; nested exception is java.lang.NoSuchMethodError: org.jboss.logging.Logger.debugf
So even though I don't have jboss logging in my project, commons-logging is finding it somewhere and attempting to use it, but unsuccessfully. After some searching I found jboss-logging-3.1.4.GA.jar in Wildfly.
The reason for the error is that this is an old version of jboss-logging. I added a newer jar to the server and edited module.xml to point to it, and the error went away. That proves that commons-logging is finding jboss-logging on the server.
The problem is that I don't want jboss-logging, I want log4j. How do I force commons-logging to use log4j and ignore what is on the server?
Edit: I followed the link chrisharm put up and added these lines to the standalone.xml file :
<add-logging-api-dependencies value="false"/>
<use-deployment-logging-config value="false"/>
I also changed the module.xml to point to the older jar again, since I would rather see an error if jboss is used. When I run now I get this:
Caused by: java.lang.NoClassDefFoundError: org/jboss/logging/Logger
This may be a step in the right direction, since now Spring doesn't have access to jboss-logging, but it is still trying to use it for some reason.
https://docs.jboss.org/author/display/WFLY8/How+To#HowTo-HowdoIusemyownversionoflog4j%3F
If you need/want to include your version of log4j then you need to do the following two steps.
Disable the adding of the logging dependencies to all your deployments with the add-logging-api-dependencies attribute and disable the use-deployment-logging-config attribute OR exclude the logging subsystem in a jboss-deployment-structure.xml.
Then need to include a log4j library in your deployment.
This only works for logging in your deployment. Server logs will continue to use the logging subsystem configuration.

Netty in Spring and slf4j Logger Factory

My basic question is. How do I configure Netty to use slf4j in Spring? I keep getting the error below in Spring but not eclipse using the slf4j jars with the log4j bridge and api in the path. I am using Spring Tools suite, spring 3.2.3, and Netty 4 (post jboss).
Exception in thread "main" java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory
The program fails at the line where I instantiate the connection group after I try setting the default logging factory.
I am really new to Netty and slf4j. I have been banging my head against this one for a while and done a bit of research on Stack Overflow and the internet. I managed to get Netty working with slf4j in eclipse and used the Internal Logger Factory. I tried configuring slf4j in a similar way to log4j (A properties file with appenders).
I used the following code for the InternalLoggerFactory in both of the following ways with the appropriate imports.
InternalLoggerFactory.setDefaultFactory(new Slf4JLoggerFactory());
and
InternalLoggerFactory.setDefaultFactory(Slf4JLoggerFactory.getDefaultFactory());
These lines are placed immediately before my initialization of the connection group but I tried them in my Main App/Driver's main method as well.
Thanks for any help. I would and do really appreciate it.
´org.slf4j.LoggerFactory´ is part of ´slf4j-api-1.75.jar´, so it is likely that you have some deployment problem. -- Check that this jar is really in the classpath of you application, or if you have an web application, then check that this jar is deployed to you application server.
One other thing (that is maybe not the cause of your problem) is that you have log4j-1.2.16.jar and log4j-to-slf4j-2.0-beta8.jar. This is will lead to other problems, because log4j-to-slf4j is a log4j-to-slf4j bridge, that forward log4j loggers to slf4j. On the ohter hand you have the real log4j in your classpath too. I would remove log4j-1.2.16.16. I hope this slf4j docu describe it a bit better than me.
An other point that brothers me, is that you use different version of slf4j. I strongly recommend to use the same version for all slf4j libs!

Log4j conflicts with commons logging in a Spring RESTful integration on tomcat 6

I am trying to integrate Spring RESTful service in my project which already uses log4j. Log4j is conflicting with commonslogging and not initializing the service. I am using commons logging to compile spring jar.
If your using Maven try
<scope> provided</scope> for log4j transitive dependencies.
Issue has been resolved by including log4j.xml in my classes folder.
With all research on this topic, I come to know that jar conflict issue is with Webspehere not with Tomcat.

Resources