Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lombok @Log4j2 annotation doesn't work with latest version of log4j (v2.15.0)

I upgraded the log4j dependency to the latest 2.15.0 version and now my Spring Boot application throws an error on start up

Exception in thread "main" java.lang.NoSuchFieldError: EMPTY_BYTE_ARRAY
    at org.apache.logging.log4j.core.config.ConfigurationSource.<clinit>(ConfigurationSource.java:56)
    at org.apache.logging.log4j.core.config.NullConfiguration.<init>(NullConfiguration.java:32)
    at org.apache.logging.log4j.core.LoggerContext.<clinit>(LoggerContext.java:85)
    at org.apache.logging.log4j.core.selector.ClassLoaderContextSelector.createContext(ClassLoaderContextSelector.java:254)
    at org.apache.logging.log4j.core.selector.ClassLoaderContextSelector.locateContext(ClassLoaderContextSelector.java:218)
    at org.apache.logging.log4j.core.selector.ClassLoaderContextSelector.getContext(ClassLoaderContextSelector.java:136)
    at org.apache.logging.log4j.core.selector.ClassLoaderContextSelector.getContext(ClassLoaderContextSelector.java:123)
    at org.apache.logging.log4j.core.selector.ClassLoaderContextSelector.getContext(ClassLoaderContextSelector.java:117)
    at org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:150)
    at org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:47)
    at org.apache.logging.log4j.LogManager.getContext(LogManager.java:194)
    at org.apache.logging.log4j.LogManager.getLogger(LogManager.java:581)
    at foo.bar.org.MyApp.<clinit>(MyApp.java:13)

Here is my main class

@Log4j2
@SpringBootApplication(exclude={DataSourceAutoConfiguration.class})
public class MyApp {

  public static void main(String[] args) {
    SpringApplication.run(MyApp.class, args);
  }
}
like image 716
Rakesh Avatar asked Dec 11 '21 02:12

Rakesh


People also ask

How to use @log annotations in Lombok?

@Log, @Log4j2, @Slf4j – Lombok annotations Instead, we can start writing log statements in a java class which is annotated with lombok’s @Log annotations. Lombok supports following log annotations for spring boot – By default, spring boot uses logback as logging provider. 3. Using Log4j2 Logging with Lombok

How to use Log4j in Lombok?

Use @Log4j @Log4j creates a logger using the log4j library. Similar to previous examples we must annotate our class: Then Lombok generates the logger accordingly: 2.4. Use @Log4J2 We can also use @Log4j2 to use the newer version of log4j.

Why is @log4j2 annotation not working in idea?

Actually log4j2 works through Slf4j, because it uses log4j2.xml configs. Just wondering why annotation is not working It could be that the IDEA plug-in doesn't support @Log4j2 yet. If that's the case IDEA will show an error message, but compiling the code should work.

Is the Log4j 2 API compatible with Log4j 1?

The API for Log4j 2 is not compatible with Log4j 1.x, however an adapter is available to allow applications to continue to use the Log4j 1.x API. Adapters are also available for Apache Commons Logging, SLF4J, and java.util.logging. Log4j 2.14.1 is the latest release of Log4j. As of Log4j 2.13.0 Log4j 2 requires Java 8 or greater at runtime.


1 Answers

In case you have the following in your pom.xml:

<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-api</artifactId>
    <version>2.15.0</version>
</dependency>
<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-core</artifactId>
    <version>2.15.0</version>
</dependency>

make sure that both have the same version. I forgot it for one (left the old version) and saw the same error.

like image 70
Philipp S Avatar answered Nov 15 '22 15:11

Philipp S