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

9

38

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?

Sedum answered 22/9, 2014 at 14:7 Comment(0)
S
40

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.

Slimy answered 22/9, 2014 at 14:11 Comment(2)
And logging.apache.org/log4j/2.x/manual/migration.html might be useful to read too.Orgell
And you find tutorials no older than a year featuring guides for the old version. Beautiful.Micamicaela
P
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);
            }
        }
Philosophize answered 3/6, 2016 at 14:8 Comment(0)
S
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

Subsidiary answered 27/4, 2017 at 22:56 Comment(0)
V
5

If you are using log4j version 2.

private static final Logger LOGGER = LogManager.getLogger(TheClass.class);
Veach answered 21/3, 2018 at 6:5 Comment(0)
G
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/

Gabionade answered 22/9, 2014 at 14:20 Comment(0)
I
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.

Immuno answered 22/9, 2014 at 15:29 Comment(0)
F
1

You can remove error Method 1: import org.apache.logging.log4j.Logger; copy and paste same package ,it will solved

Method2: during importing you need not get pkg import org.apache.logging.log4j with I and C then you can import this package.see image enter image description here

Forgat answered 2/5 at 6:43 Comment(0)
O
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");
}
Oversoul answered 23/4, 2018 at 8:29 Comment(0)
M
0

This worked for me:

import org.apache.logging.log4j.Logger;
private static final Logger log = LogManager.getLogger(YourClass.class);
Mahican answered 13/9, 2023 at 23:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.