Skip to content
Advertisement

slf4j in library seems to ignore my log4j2 configuration

We use Log4j2 in our java-ee application. We use a library, where logging is programmed against SLF4J. In this library is a class, which logs a lot of stuff I do not want -> so I want to set LogLevel of this Logger to OFF.

My log4j2.xml looks like this:

JavaScript

But BaseSerializingTranscoder still logs errors. If I do a simple test and place the log code found in BaseSerializingTranscoder to a test function, I see that the logger retrieved over org.slf4j.Logger.LoggerFactory (what is done by the library) has not the same configuration as if I retrieve the logger over org.apache.logging.log4j.LogManager (where my configuration is applied correctly):

JavaScript

The output of logger1 is visible, which I wanted to eliminate. What is wrong?

Edit
We use Wildfly 10.1. I was able to create a minimal example which has exactly the same problem.

pom.xml:

JavaScript

log4j2.xml:

JavaScript

Service.java:

JavaScript

output:

JavaScript

test3 and test4 should not be logged! What is wrong?

Advertisement

Answer

UPDATE #2:

After your updates I was able to duplicate the problem. As per my latest comment I was able to use this answer to guide me in fixing the issue.

I added the following jboss-deployment-structure.xml to the META-INF of the web project:

JavaScript

I also modified the log4j2.xml slightly so that I would be sure without any doubt that the configuration specified by this file was being used. I changed the PatternLayout to add the string “MY_PATTERN” as a prefix for every message:

<PatternLayout pattern="MY_PATTERN %d %p %c [%t] %m%n" />

Output:

JavaScript

The messages test3 and test4 are no longer logged to console.


PREVIOUS Answer and Update:

I suspect your issue has to do with how you have your classpath configured. I highly recommend that you check your dependency hierarchy to make sure you have all the correct logging libraries you need.

Here is a simple example of using the OFF level with SLF4J and log4j2:

JavaScript

Here is the log4j2.xml configuration:

JavaScript

Here are the dependencies:

JavaScript

When I have the level of the “blah” logger set to off I see no logs on the console at all. When I change it to something like trace I see all of the logs.

UPDATE:

I was able to put together a simple web application using servlet API 3.1.0 and got the logging working (I am able to set level to OFF and not see ERROR level messages) with the dependencies I mentioned in the example above. So it would certainly seem to be a configuration problem of some kind. Just as an FYI I deployed my simple app to Tomcat 8.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement