Java 方法 getLogger() 不再是 log4j2 中 Logger 的成员?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25976120/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 01:37:19  来源:igfitidea点击:

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

javalogginglog4jlog4j2

提问by huahsin68

I have the log4j-api-2.0.0.jarand log4j-core-2.0.2.jarimport into my build path. But somehow the following code were fail:

我有log4j-api-2.0.0.jarlog4j-core-2.0.2.jar导入到我的构建路径中。但不知何故,以下代码失败了:

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

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?

我只是很好奇getLogger()不再是 Logger 中的有效方法?

采纳答案by Sotirios Delimanolis

You'll notice Loggerno longer declares such a method.

您会注意到Logger不再声明这样的方法。

log4j version 2 has made some drastic changes. Here'sthe change log. getLoggerseems to have been moved to a LogManagerclass.

log4j 版本 2 进行了一些重大更改。这是更改日志。getLogger似乎已经被转移到一个LogManager班级。

Here'show they suggest making the migration.

以下是他们建议进行迁移的方式。

回答by pd30

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

是的,您的观察是正确的。它不支持 getLogger() 方法。

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

检查此文档链接:http: //logging.apache.org/log4j/2.x/log4j-core/apidocs/index.html

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

示例教程:http: //www.javabeat.net/log4j-2-example/

回答by Remko Popma

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

正如其他答案中所述,Logger现在是一个接口,您可以从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.

API 现在与实现分离,让团队可以在不破坏用户代码的情况下自由更改实现。API 很少更改,如果更改,它将是 2.x 版本,而不是 2.0.x 版本。也就是说,始终使用匹配的 log4j-api 和 log4j-core 版本可能是个好主意。

回答by RajeeV VenkaT

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);
            }
        }

回答by Ishimwe Aubain Consolateur

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;

对于新的 Log4J 2,您必须至少添加(在我的情况下)log4j-core-2.8.2、log4j-api-2.8.2,在其他一些情况下,您可能还需要添加 log4j-web-2.8.2。因此,当您想要获取日志时,请导入 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());

最后的用法是 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

希望这会帮助某人

回答by Pranav V R

You are using log4j version 2.

您正在使用 log4j 版本 2。

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

回答by Tadele Ayelegn

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

要在您的类上使用 getLogger() 方法,请导入Logger

import java.util.logging.Logger;

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.log4jpackage as seen below

或者使用org.apache.log4j包中的Logger,如下所示

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");
}