1
votes

I am in the process of migrating from log4j 1.2 to log4j 2.

I have my whole configuration into a log4j2.properties file.

I noticed that I got a new error message in my logs while starting my tomcat :

log4j:WARN No appenders could be found for logger (org.springframework.web.filter.CharacterEncodingFilter). log4j:WARN Please initialize the log4j system properly. log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.

I have verified with my previous logs, and I did not have this warning previously.

Here is my previous configuration (in the log4j.properties file):

log4j.rootLogger=INFO, CONSOLE

log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%d{dd-MM HH:mm:ss} %p %t %c - %m%n
log4j.logger.com.example=DEBUG
log4j.logger.org.springframework=INFO

Here is the new one (in log4j2.properties) :

appender.console.type = Console
appender.console.name = ConsoleAppender
appender.console.layout.type = PatternLayout
appender.console.layout.pattern = %d %p %t %c - %m%n

loggers = log1, log2

logger.log1.name = org.springframework
logger.log1.level = INFO
logger.log1.appenderRef = ConsoleAppender

logger.log2.name = com.example
logger.log2.level = DEBUG
logger.log2.appenderRef = ConsoleAppender

rootLogger.level = ERROR
rootLogger.appenderRef.stdout.ref = ConsoleAppender

So, what I've understand so far is :

  • I create one appender, named ConsoleAppender, of type Console
  • I create two loggers that goes each onto ConsoleAppender, one on org.springframework (level INFO) and the other on com.example (level INFO)

I use this page to try to understand how to do the migration.

Do you see any explanation about why I should have this particular message ?

1
Did you try to specify the config file location? I think that error means that Log4j is not finding it: logging.apache.org/log4j/2.0/faq.html#config_locationxuso
It seems there is no need to specify it, as log4j2.properties is automatically found (as log4j.properties was automatically found with the previous version)Louis V
Reading again the log message, I noticed that is the 1.2 version who is not finding its configuration, not the 2.4 one. Maybe the old 1.2 jar is still in your classpath?xuso

1 Answers

2
votes

As seen in this answer, the issue may be due to the fact that I was also using slf4j-log4j12 (but in the latest version already) and it was in conflict with other log4j apis :

<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-api</artifactId>
    <version>1.7.25</version>
</dependency>
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-log4j12</artifactId>
    <version>1.7.25</version>
</dependency>

So I did what's told and I removed the org.slf4j:slf4j-log4j12 dependancy and added log4j-api & log4j-slf4j-impl dependancies :

<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-core</artifactId>
    <version>2.8.2</version>
</dependency>
<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-api</artifactId>
    <version>2.8.2</version>
</dependency>
<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-slf4j-impl</artifactId>
    <version>2.8.2</version>
</dependency>

... and for know it seems to solve the error (it doesn't appear anymore into my starting logs

To be more clear, I first only added the three above dependancies without removing the slf4j-log4j12 one, and I got this as well into the logs :

SLF4J: Found binding in [jar:file:/myuser/apache-tomcat-7.0.75/webapps/mywebapp/WEB-INF/lib/log4j-slf4j-impl-2.8.2.jar!/org/slf4j/impl/StaticLoggerBinder.class] SLF4J: Found binding in [jar:file:/myuser/apache-tomcat-7.0.75/webapps/mywebapp/WEB-INF/lib/slf4j-log4j12-1.7.25.jar!/org/slf4j/impl/StaticLoggerBinder.class]

After having removed the slf4j-log4j12 dependancy, the whole error logs disappeared.