35

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?

Remko Popma
  • 35,130
  • 11
  • 92
  • 114
huahsin68
  • 6,819
  • 20
  • 79
  • 113

7 Answers7

39

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.

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
  • 2
    And http://logging.apache.org/log4j/2.x/manual/migration.html might be useful to read too. – nos Sep 22 '14 at 14:13
  • 1
    And you find tutorials no older than a year featuring guides for the old version. Beautiful. – ASA Sep 03 '15 at 17:54
12

I'm giving an example for better understanding.

private static Logger logger;
        static {
            try {   
                   // you need to do something like below instaed of Logger.getLogger(....);
                    logger = LogManager.getLogger(ResourceService.class); 
              } catch (Throwable th) {
                    throw new HRException("Cannot load the log property file", th);
            }
        }
Rajeev
  • 442
  • 1
  • 5
  • 18
6

with new Log4J 2, you have to add at least (in my case) log4j-core-2.8.2, log4j-api-2.8.2 and in some other case you may need to add also log4j-web-2.8.2. So when you want to get a logging you import import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger;

and finally the usage will be static final Logger LOGGER = LogManager.getLogger(WebService.class.getName());

Note: don't forget to put config file into the root directory of the project, other wise you won't be able to get your logs.

Hope this will help someone Kind regards

4

If you are using log4j version 2.

private static final Logger LOGGER = LogManager.getLogger(TheClass.class);
pvrforpranavvr
  • 2,708
  • 2
  • 24
  • 34
2

Yes your observation is correct.It does not support getLogger() method.

Check this Documentation link out: http://logging.apache.org/log4j/2.x/log4j-core/apidocs/index.html

Sample tutorial:http://www.javabeat.net/log4j-2-example/

pd30
  • 240
  • 1
  • 7
2

As noted in other answers, Logger is now an interface and you can get Logger instances from the LogManager.

API is now separate from the implementation, to give the team the freedom to change the implementation without breaking user code. The API will rarely change, and if it does change it will be in a 2.x version, not a 2.0.x version. That said, it is probably a good idea to always use matching log4j-api and log4j-core versions.

Remko Popma
  • 35,130
  • 11
  • 92
  • 114
0

To use the getLogger() method on your class, import the Logger class

import java.util.logging.Logger;

and use it as follows

public class SpringBoot {
  private static final Logger LOGGER = Logger.getClass("SpringBoot");
}

And remember, this method takes a string argument.

Or Use Logger from org.apache.log4j package as seen below

import org.apache.log4j.Logger;

public class MessageProcessor {

private static final Logger LOGGER = Logger.getLogger(MessageProcessor.class);

public void processMessage(Message<String> msg) {
    LOGGER.info("Message is about to be processed");
}
Tadele Ayelegn
  • 4,126
  • 1
  • 35
  • 30