I'm using log4j2 (2.11.1) with Java 11 and attempting to get a Logger
object using:
private static final Logger LOG = LogManager.getLogger();
(Imported from log4j-api
in org.apache.logging.log4j
)
At runtime, I receive the following error:
WARNING: sun.reflect.Reflection.getCallerClass is not supported. This will impact performance. Exception in thread "main" java.lang.ExceptionInInitializerError Caused by: java.lang.UnsupportedOperationException: No class provided, and an appropriate one cannot be found. at org.apache.logging.log4j.LogManager.callerClass(LogManager.java:555) at org.apache.logging.log4j.LogManager.getLogger(LogManager.java:580) at org.apache.logging.log4j.LogManager.getLogger(LogManager.java:567) at app.App.<clinit>(App.java:11)
Which does make sense - getCallerClass is not supported and so the logger is unable to determine the class name.
Is it supposed to work this way? Surely I don't have to hard-code the class name into each logger?
The getLogger () method of java.util.logging.LogManager is used to get the specified Logger in this LogManager instance. This Logger must be a named Logger. This method will get this Logger in this LogManager if it exists. If it does not exists, then this method returns null.
The most common usage of this class is to obtain a named Logger. The method getLogger () is provided as the most convenient way to obtain a named Logger based on the calling class name.
The method getLogger (Class<TheClass>) is undefined for the type Logger I am just so curious is getLogger () no longer a valid method in Logger? You'll notice Logger no longer declares such a method. log4j version 2 has made some drastic changes. Here's the change log. getLogger seems to have been moved to a LogManager class.
To use the getLogger () method on your class, import the Logger class public class SpringBoot { private static final Logger LOGGER = Logger.getClass ("SpringBoot"); } And remember, this method takes a string argument.
The reason was that the multi-release class files were not being picked up from META-INF/versions/*
because I hadn't set the multi-release flag when I built my shaded jar.
I needed to add:
Multi-Release:true
To my manifest, and everything started working.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With