I have the log4j-api-2.0.0.jar
and log4j-core-2.0.2.jar
import into my build path. But somehow the following code were fail:
import org.apache.logging.log4j.core.Logger; public class TheClass { private static Logger log = Logger.getLogger(TheClass.class); ...
And the error message shows that:
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?
The getLogger() method of a Logger class used find or create a logger. If there is a logger exists with the passed name then the method will return that logger else method will create a new logger with that name and return it. There are two types of getLogger() method depending upon no of the parameter passed.
Logger Hierarchy is made up of set of LoggerConfig objects with a parent-child relationship. The topmost element in every Logger Hierarchy is the Root Logger. If Log4j2 doesn't find the configuration file, only Root Logger will be used for logging with logging level as ERROR.
getLogger() (without passing a name), then it simply will return the root logger which is created at import time (and therefore, it is never new).
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.
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.
Here's how they suggest making the migration.
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