Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LogManager.getLogger() is unable to determine class name on Java 11

Tags:

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?

like image 391
Daniel Scott Avatar asked Oct 23 '18 16:10

Daniel Scott


People also ask

How to get the specified logger in a Java logmanager?

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.

How to get a named logger from a calling class?

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.

Is getlogger() no longer a valid method in logger?

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.

How do I use the getlogger() method on my 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.


1 Answers

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.

like image 135
Daniel Scott Avatar answered Sep 22 '22 15:09

Daniel Scott