Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Method getLogger() no longer a member of Logger in log4j2?

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?

like image 694
huahsin68 Avatar asked Sep 22 '14 14:09

huahsin68


People also ask

What is logger getLogger ()?

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.

What is Loggers in Log4j2?

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.

What does getLogger return?

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).

What does LogManager getLogger do?

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.


1 Answers

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.

like image 186
Sotirios Delimanolis Avatar answered Oct 13 '22 23:10

Sotirios Delimanolis