创建记录器时Log4j 2挂起 [英] Log4j 2 hangs when creating logger

查看:126
本文介绍了创建记录器时Log4j 2挂起的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的程序:

package myPackage;

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

public class Test {
    private static Logger logger; 

    public static void main(String[] args) throws Exception {
        System.out.println("Creating logger...");
        logger = LogManager.getLogger(Test.class); 
        System.out.println("Logger created.");
        logger.info("Hello world!");
    }
}

如果我在调试器下运行程序,它将打印:

If I run the program under the debugger, it prints:

Creating logger...

然后挂起.

如果我在没有调试器的情况下运行该程序,则会显示:

If I run the program without debugger, it prints:

Exception in thread "main" java.lang.StackOverflowError
    at sun.reflect.Reflection.quickCheckMemberAccess(Reflection.java:84)
    at java.lang.reflect.Method.invoke(Method.java:489)
    at org.apache.logging.log4j.util.ReflectionUtil.getCallerClass(ReflectionUtil.java:128)
    at org.apache.logging.log4j.util.ReflectionUtil.getCallerClass(ReflectionUtil.java:205)
    at org.apache.logging.slf4j.Log4jLoggerFactory.getContext(Log4jLoggerFactory.java:42)
    at org.apache.logging.log4j.spi.AbstractLoggerAdapter.getLogger(AbstractLoggerAdapter.java:42)
    at org.apache.logging.slf4j.Log4jLoggerFactory.getLogger(Log4jLoggerFactory.java:29)
    at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:329)
    at org.apache.logging.slf4j.SLF4JLoggerContext.getLogger(SLF4JLoggerContext.java:42)
    at org.apache.logging.slf4j.Log4jLoggerFactory.newLogger(Log4jLoggerFactory.java:37)
    at org.apache.logging.slf4j.Log4jLoggerFactory.newLogger(Log4jLoggerFactory.java:29)
    at org.apache.logging.log4j.spi.AbstractLoggerAdapter.getLogger(AbstractLoggerAdapter.java:48)
    at org.apache.logging.slf4j.Log4jLoggerFactory.getLogger(Log4jLoggerFactory.java:29)
    at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:329)
    [... more of the same]

我正在使用Log4j 2版本2.5和SLF4J版本1.7.18.

I am using Log4j 2 version 2.5 along with SLF4J version 1.7.18.

这是我的课程路径:

// JRE 1.8.0_74

// Log4j 2
log4j-1.2-api-2.5.jar
log4j-1.2-api-2.5-javadoc.jar
log4j-1.2-api-2.5-sources.jar
log4j-api-2.5.jar
log4j-api-2.5-javadoc.jar
log4j-api-2.5-sources.jar
log4j-core-2.5.jar
log4j-core-2.5-javadoc.jar
log4j-core-2.5-sources.jar
log4j-core-2.5-tests.jar
log4j-flume-ng-2.5.jar
log4j-flume-ng-2.5-javadoc.jar
log4j-flume-ng-2.5-sources.jar
log4j-iostreams-2.5.jar
log4j-iostreams-2.5-javadoc.jar
log4j-iostreams-2.5-sources.jar
log4j-jcl-2.5.jar
log4j-jcl-2.5-javadoc.jar
log4j-jcl-2.5-sources.jar
log4j-jmx-gui-2.5.jar
log4j-jmx-gui-2.5-javadoc.jar
log4j-jmx-gui-2.5-sources.jar
log4j-jul-2.5.jar
log4j-jul-2.5-javadoc.jar
log4j-jul-2.5-sources.jar
log4j-nosql-2.5.jar
log4j-nosql-2.5-javadoc.jar
log4j-nosql-2.5-sources.jar
log4j-slf4j-impl-2.5.jar
log4j-slf4j-impl-2.5-javadoc.jar
log4j-slf4j-impl-2.5-sources.jar
log4j-taglib-2.5.jar
log4j-taglib-2.5-javadoc.jar
log4j-taglib-2.5-sources.jar
log4j-to-slf4j-2.5.jar
log4j-to-slf4j-2.5-javadoc.jar
log4j-to-slf4j-2.5-sources.jar
log4j-web-2.5.jar
log4j-web-2.5-javadoc.jar
log4j-web-2.5-sources.jar

// SLF4J 1.7.18
slf4j-api-1.7.18.jar

推荐答案

您的jar数量超过了您的需要.特别是,您的应用程序未使用SLF4J,因此您无需将其包括在内.但是您还包括了log4j-slf4j,log4j-core和log4j-slf4j-impl. Log4g-core是实际的Log4j 2实现. log4j-to-slf4j是Log4j 2 API的实现,该API将所有事件路由到SLF4J.然后log4j-slf4j-impl将请求路由回Log4j api,循环将重新开始.

You have more jars than you need. In particular, your application is not using SLF4J, so you don't need to include it. But you have also included log4j-slf4j, log4j-core and log4j-slf4j-impl. Log4g-core is the actual Log4j 2 implementation. log4j-to-slf4j is an implementation of the Log4j 2 API that routes all events to SLF4J. log4j-slf4j-impl will then route the requests back to the Log4j api and the loop will start all over again.

在类路径中不能同时包含log4j-to-slf4j和log4j-slf4j-impl jar.如果您希望Log4j 2进行日志记录,请删除log4j-to-slf4j.

You cannot have both the log4j-to-slf4j and log4j-slf4j-impl jars in the classpath. If you want Log4j 2 to do your logging then remove log4j-to-slf4j.

此外,您的示例应用程序也不是Web应用程序,因此您不应在类路径中使用log4j-web.

Also, your sample application is not a web application so you should not have log4j-web in the classpath.

最后,除非您真的想要所有可选组件,例如flume,no-sql和jmx,否则不应包括它们.

Finally, unless you really want all the optional components like flume, no-sql, and jmx you should not include them.

这篇关于创建记录器时Log4j 2挂起的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆