log4j2 ERROR Unrecognized format specifier [t]
Asked Answered
C

3

11

I have a log4j2 configuration file for a web app running on Tomcat 8 that looks like this

<?xml version="1.0" encoding="UTF-8"?>
<Configuration monitorInterval="30" status="trace" strict="true">
        <Properties>
                <Property name="logdir">/path/to/log/dir</Property>
                <Property name="filename">somelogfile.log</Property>
        </Properties>
        <Loggers>
                <Logger name="some.package.name" level="debug" additivity="false">
                        <AppenderRef ref="RollingFile"/>
                </Logger>
        </Loggers>
        <Appenders>
                <RollingFile name="RollingFile" fileName="${logdir}/${filename}" filePattern="${logdir}/${filename}.%d{yyyyMMdd}.gz">
                        <PatternLayout>
                                <Pattern>%d %p %c{1.} [%t] %m%n</Pattern>
                        </PatternLayout>
                        <Policies>
                                <TimeBasedTriggeringPolicy interval="6" modulate="true"/>
                        </Policies>
                </RollingFile>
        </Appenders>
</Configuration>

I see that my web app writes to the log file, but the %t pattern I have for thread name doesn't seem to resolve, so I get log statements like this

2017-06-10 20:34:51,696 DEBUG s.p.n.SomeServlet [%t] some log message

Notice I get %t instead of the thread name

So to troubleshoot this I started Tomcat using the option

-Dorg.apache.logging.log4j.simplelog.StatusLogger.level=TRACE

and I see the following messages print in catalina.out when the webapp is deploying and log4j2 is initializing.

ERROR StatusLogger Unrecognized format specifier [thread]
ERROR StatusLogger Unrecognized conversion specifier [thread] starting at position 25 in conversion pattern
2017-06-10 19:51:14,277 localhost-startStop-1 DEBUG PluginManager 'Converter' found 41 plugins
2017-06-10 19:51:14,609 localhost-startStop-1 ERROR Unrecognized format specifier [t]
2017-06-10 19:51:14,614 localhost-startStop-1 ERROR Unrecognized conversion specifier [t] starting at position 6 in conversion pattern.

my web app has the following jar files among others

WEB-INF/lib/log4j-api-2.8.2.jar
WEB-INF/lib/log4j-core-2.8.2.jar
WEB-INF/lib/log4j-web-2.8.2.jar

Not sure what is causing %t to print instead of the actual thread name.

.

Cleary answered 11/6, 2017 at 3:48 Comment(1)
Maybe a Log4J2 bug issues.apache.org/jira/browse/LOG4J2-954 . But I cannot reproduce your problem although I use log4j 2.8.2. The result is like 2017-06-12 16:22:29,050 INFO s.p.name [main] some log message.Itinerate
C
7

The only time I've seen something similar was when the classpath contained multiple versions of Log4j2. I didn't investigate to find the root cause though.

Cognizance answered 12/6, 2017 at 23:27 Comment(1)
In case you use the maven-shade-plugin to build a fat jar, the maven-shaded-log4j-transformer plugin might be helpful: github.com/edwgiz/maven-shaded-log4j-transformerSolubility
V
5

In case you are using ShadowJar and Gradle and you don´t have multiple Log4J versions as in other answers assumed, try this which solves the issue (requires Shadow Plugin > 4.0.0, otherwise see this Plugin ):

build.gradle

import com.github.jengelman.gradle.plugins.shadow.transformers.Log4j2PluginsCacheFileTransformer

shadowJar{
    transform(Log4j2PluginsCacheFileTransformer)
}
Vulgar answered 22/1, 2020 at 10:1 Comment(0)
C
1

I too experienced this when I had multiple versions of log4j2 running. Check if any of your dependencies pull in duplicate jars..good luck! Can be a nightmare with classloaders!

Cas answered 14/6, 2017 at 22:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.