I am trying to initialize log4j2 in my servlet application (apache-tomcat 9). I am getting a series of exceptions
2024-09-26T07:58:02.478967Z main ERROR Unable to create Lookup for bundle java.lang.ClassCastException: class org.apache.logging.log4j.core.lookup.ResourceBundleLookup
at java.base/java.lang.Class.asSubclass(Class.java:3924)
2024-09-26T07:58:02.480089Z main ERROR Unable to create Lookup for ctx java.lang.ClassCastException: class org.apache.logging.log4j.core.lookup.ContextMapLookup
at java.base/java.lang.Class.asSubclass(Class.java:3924)
With series of
2024-09-26T07:58:07.538777Z main ERROR Unrecognized format specifier [msg]
2024-09-26T07:58:07.538808Z main ERROR Unrecognized conversion specifier [msg] starting at position 54 in conversion pattern.
2024-09-26T07:58:07.538844Z main ERROR Unrecognized format specifier [n]
2024-09-26T07:58:07.538875Z main ERROR Unrecognized conversion specifier [n] starting at position 56 in conversion pattern.
In my code I have the log4j2.properties in this folder
src/main/webapp/WEB-INF/
This is my log4j2.properties content
# Set root logger level to DEBUG and its only appender to Console
log4j.rootLogger=DEBUG, Console
# Console appender configuration
log4j.appender.Console.type=Console
log4j.appender.Console.name=STDOUT
log4j.appender.Console.layout.type=PatternLayout
log4j.appender.Console.layout.pattern=%d{yyyy-MM-dd HH:mm:ss} [%t] %-5p %c{1} - %m%n
# Define appenders
appender.console.type = Console
appender.console.name = CONSOLE
appender.console.layout.type = PatternLayout
appender.console.layout.pattern = %highlight{%d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n}{FATAL=red, ERROR=red, WARN=yellow, INFO=green, DEBUG=blue}
appender.file.type = File
appender.file.name = FILE
appender.file.fileName = logs/application.log
appender.file.layout.type = PatternLayout
appender.file.layout.pattern = %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n
# Define loggers
rootLogger.level = INFO
rootLogger.appenderRef.console.ref = CONSOLE
rootLogger.appenderRef.file.ref = FILE
logger.my.package.first.name = my.package.first
logger.my.package.first.level = DEBUG
logger.my.package.first.appenderRef.file.ref = FILE
logger.my.package.second.name = my.package.second
logger.my.package.second.level = DEBUG
logger.my.package.second.appenderRef.file.ref = FILE
My src/main/webapp/WEB-INF/web.xml
has the following content
<listener>
<listener-class>org.apache.logging.log4j.web.Log4jServletContextListener</listener-class>
</listener>
<context-param>
<param-name>log4jConfiguration</param-name>
<param-value>log4j2.properties</param-value>
</context-param>
<context-param>
<param-name>log4j2.debug</param-name>
<param-value>true</param-value>
</context-param>
<context-param>
<param-name>log4j2.verbose</param-name>
<param-value>true</param-value>
</context-param>
<context-param>
<param-name>log4j.shutdownHookEnabled</param-name>
<param-value>true</param-value>
</context-param>
I have downloaded the following files and copied it to the tomcat/lib folder
- log4j-web-2.24.0
- log4j-core-2.24.0
- log4j-api-2.24.0
- log4j-jul-2.24.0
I have referred to many links on SO
- One link hinted that unable to find log4j2.properties file, so I copied log4j2.properties to lib, conf, webapps, webapps/myapp, webapps/myapp/classes folders and checked, no luck same error
- Other links hinted that content may in incorrect
What am I missing?
I wont be able to exclude the .dat file as per this link since the init is happening via web.xml
I found the root cause. I had this dependency causing the issue. Once I removed it, the problem went away.
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j2-impl</artifactId>
<version>2.24.0</version>
</dependency>