Java:log4j 初始化错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12764362/
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-10-31 10:07:15  来源:igfitidea点击:

Java: log4j initialization error

javalogginglog4j

提问by John Rumpel

I'm new in using the log4j package and I don't see the error: This is a very simple and straightforward code sample:

我是使用 log4j 包的新手,我没有看到错误:这是一个非常简单直接的代码示例:

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class TestLogger {

    private static Logger logger;

    public static void main(String[] args) {

        logger = LogManager.getLogger(TestLogger.class);
        logger.info("Hello");

    }
}

When I try to compile I get this error:

当我尝试编译时出现此错误:

Exception in thread "main" java.lang.NullPointerException at org.apache.logging.log4j.LogManager.getLogger(LogManager.java:129) at my.package.logging.TestLogger.main(TestLogger.java:15)

在 org.apache.logging.log4j.LogManager.getLogger(LogManager.java:129) 在 my.package.logging.TestLogger.main(TestLogger.java:15) 的线程“main”java.lang.NullPointerException 中的异常

I am wondering what on earth it's all supposed to mean ...

我想知道这到底是什么意思......

Could you help here?

你能在这里帮忙吗?

采纳答案by betomontejo

You're most probably missing one of the dependencies. This is easy reproducible if you only include in your classpath the log4j-api-2.0-beta1.jarfile.

您很可能缺少依赖项之一。如果您只在类路径中包含该log4j-api-2.0-beta1.jar文件,这很容易重现。

If you downloaded the binary distribution, include also the log4j-core-2.0-beta1.jarin your classpath. If your using Maven, put this in your pom.xml:

如果您下载了二进制分发版,请log4j-core-2.0-beta1.jar在您的类路径中也包含。如果您使用 Maven,请将其放入您的pom.xml

  <dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-api</artifactId>
    <version>2.0-beta1</version>
  </dependency>
  <dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-core</artifactId>
    <version>2.0-beta1</version>
  </dependency>

回答by SixDoubleFiveTreeTwoOne

I solved this problem by avoiding multiple copies on the classpath of the log4j2 jars.

我通过避免 log4j2 jar 的类路径上的多个副本解决了这个问题。

The NPE occurs at Line 129 of LogManager. (return statement = Line 129)

NPE 发生在 LogManager 的第 129 行。(返回语句 = 第 129 行)

/**
 * Return a Logger with the specified name.
 *
 * @param name The logger name.
 * @return The Logger.
 */
public static Logger getLogger(String name) {

    return factory.getContext(LogManager.class.getName(), null, false).getLogger(name);
}

The Problem seems to be, that the factory (LoggerContextFactory) is null = not initialized. Initialization takes place in a static initializer block in the class LogManager.

问题似乎是,工厂 (LoggerContextFactory) 为 null = 未初始化。初始化发生在类 LogManager 的静态初始化块中。

My assumption is, that initialization failes when there is more than one LogManager on the classpath.

我的假设是,当类路径上有多个 LogManager 时,初始化会失败。

In my project (a webapplication developed with eclipse) i had the jars in my WEB-INF/lib andreferenced by a dependency to another project. Making ALL references point to the same jars solved that issue in my case.

在我的项目(使用Eclipse开发web应用),我不得不罐子在我的WEB-INF / lib目录,并通过依赖于另一个项目中引用。在我的情况下,使所有引用都指向相同的 jars 解决了这个问题。

回答by DNA

The following works for me (updated: now the same as your original posting, but showing the imports)

以下对我有用(更新:现在与您的原始帖子相同,但显示导入)

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;

public class TestLogger
{
    private static Logger logger;

    public static void main(String[] args) {
        logger = LogManager.getLogger(TestLogger.class);
        logger.info("Hello");
    }
}

provided that you have created a log4j configuration, otherwise you'll get the dreaded error:

前提是你已经创建了一个 log4j 配置,否则你会得到可怕的错误:

log4j:WARN No appenders could be found for logger (TestLogger).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.