2

Spring 4.3.30+hibernate 5 application. pom.xml has all the log4j2 dependencies.

 <dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-jcl</artifactId>
    <version>${log4j2.version}</version>
  </dependency>
  <dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-core</artifactId>
    <version>${log4j2.version}</version>
  </dependency>
  <dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-slf4j-impl</artifactId>
    <version>${log4j2.version}</version>
  </dependency>
  <dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-api</artifactId>
    <version>${log4j2.version}</version>
  </dependency>
  <dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-1.2-api</artifactId>
    <version>${log4j2.version}</version>
  </dependency>
  <dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-to-slf4j</artifactId>
    <version>${log4j2.version}</version>
  </dependency>

even added log4j2.xml

<?xml version="1.0" encoding="UTF-8" ?>
  <Configuration status="TRACE">

  <Appenders>
    <Console name="Console" target="SYSTEM_OUT">
      <PatternLayout pattern="%p [%t] %c{1}.%M(%L) | %m%n"/>
    </Console>
  </Appenders>
  <Loggers>
    <Root level="INFO">
      <AppenderRef ref="Console"/>
    </Root>
  </Loggers>

</Configuration>

The ERROR i have been getting:

ERROR StatusLogger Unrecognized format specifier [d]
ERROR StatusLogger Unrecognized conversion specifier [d] starting at position 16 in conversion pattern.
ERROR StatusLogger Unrecognized format specifier [thread]
ERROR StatusLogger Unrecognized conversion specifier [thread] starting at position 25 in conversion pattern.
ERROR StatusLogger Unrecognized format specifier [level]
ERROR StatusLogger Unrecognized conversion specifier [level] starting at position 35 in conversion pattern.
ERROR StatusLogger Unrecognized format specifier [logger]
ERROR StatusLogger Unrecognized conversion specifier [logger] starting at position 47 in conversion pattern.
ERROR StatusLogger Unrecognized format specifier [msg]
ERROR StatusLogger Unrecognized conversion specifier [msg] starting at position 54 in conversion pattern.
ERROR StatusLogger Unrecognized format specifier [n]
ERROR StatusLogger Unrecognized conversion specifier [n] starting at position 56 in conversion pattern.
ERROR StatusLogger Unrecognized format specifier [d]
ERROR StatusLogger Unrecognized conversion specifier [d] starting at position 16 in conversion pattern.
ERROR StatusLogger Unrecognized format specifier [thread]
ERROR StatusLogger Unrecognized conversion specifier [thread] starting at position 25 in conversion pattern.
ERROR StatusLogger Unrecognized format specifier [level]
ERROR StatusLogger Unrecognized conversion specifier [level] starting at position 35 in conversion pattern.

What else am i missing here as i keep getting above error even though my pattern doesn't contain the default layout? I was using log4j before migrating to log4j2.Din't plan on migrating to log4j2 but as i had to upgrade spring and hibernate i started getting ERROR StatusLogger No Log4j2 configuration file found. That's when i decided to migrate to log4j2.

1 Answers1

3

First, lets list your dependencies and what they do:

  1. log4j-jcl provides an implementation for Apache Commons Logging to route those logging calls to Log4j 2.
  2. log4j-core is the Log4j 2 implementation.
  3. log4j-slf4j-impl routes logging calls using the SLF4J API to Log4j 2.
  4. log4j-api is the Log4j 2 API.
  5. log4j-1.2-api routes logging calls made using log4j 1.x to Log4j 2.
  6. log4j-to-slf4j routes logging calls from the Log4j 2 API to SLF4J.

In case it isn't clear from the descriptions, item 6 is going to be a problem. First, you now have 2 implementations of the Log4j 2 API - log4j-core and log4j-to-slf4j. If log4j-to-slf4j were the one to "win" you would end up with logging going nowhere as the calls would bounce between Log4j and SLF4J and back again.

However, if these are in your dependencyManagement section of the the pom that would be a different story as all that does is declare which version of the jars you want to use.

As for the errors you are seeing, that happens because Log4j's core plugins aren't being loaded for some reason. That can happen if you are shading everything into a single jar and didn't include Log4j's Log4jPlugins.dat file.

rgoers
  • 8,696
  • 1
  • 22
  • 24
  • as i was getting the format specifier errors i added **/Log4j2Plugins.dat as mentioned in this : https://stackoverflow.com/questions/48033792/log4j2-error-statuslogger-unrecognized-conversion-specifier But i still keep getting the error – Aishwarya Patil Apr 18 '21 at 18:09
  • 1
    If you remove the Log4j2Plugins.dat file then you will have to add a packages attribute to the configuration element that lists all the packages that might contain Log4j plugins. This is NOT recommended as class path scanning is slow. Are you creating a shaded jar file? If you are then use https://github.com/edwgiz/maven-shaded-log4j-transformer to create a replacement Log4j2Plugins.dat. – rgoers Apr 19 '21 at 07:54
  • 1
    i tried adding annotation processors and it worked. https://logging.apache.org/log4j/2.x/manual/plugins.html – Aishwarya Patil Apr 19 '21 at 19:05
  • I just ran into this issue with a Docker container-based app on log4j2 version 2.17.2. I have a combined Clojure and Java service that is built using Leiningen 2.9.8. I still need to digest why this was broken, but downgrading to 2.17.1 fixed the issue. Before that the container continuously failed to start. – Primordinal May 27 '22 at 06:08
  • I was able to fix my issue using this Leiningen plugin during the uberjar build: https://github.com/arctype-co/log4j2-plugins-cache – Primordinal May 27 '22 at 18:12
  • flawless answer! avoid log4j-to-slf4j – Gaurav Nov 17 '22 at 10:40