java 由于 log4j 无法使用 logback

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

coudnt use logback because of log4j

javaslf4jlogback

提问by Tomasz Cy-man

Hi i have problems with logback and slf4j, im writing simple app that later is packaging in jar and i want to add there logging using logback

嗨,我在使用 logback 和 slf4j 时遇到问题,我正在编写简单的应用程序,稍后将其打包在 jar 中,我想使用 logback 添加日志记录

im using:

我正在使用:

    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>1.7.7</version>
    </dependency>
    <dependency>
        <groupId>ch.qos.logback</groupId>
        <artifactId>logback-classic</artifactId>
        <version>1.1.3</version>
    </dependency>
    <dependency>
        <groupId>ch.qos.logback</groupId>
        <artifactId>logback-core</artifactId>
        <version>1.1.3</version>
    </dependency>
    <dependency>
        <groupId>org.apache.activemq</groupId>
        <artifactId>activemq-client</artifactId>
        <version>5.8.0</version>
    </dependency>

    <dependency>
        <groupId>com.mchange</groupId>
        <artifactId>c3p0</artifactId>
        <version>${c3p0.version}</version>
    </dependency>
    <dependency>
        <groupId>commons-pool</groupId>
        <artifactId>commons-pool</artifactId>
        <version>${commons.pool.version}</version>
    </dependency>
    <dependency>
        <groupId>postgresql</groupId>
        <artifactId>postgresql</artifactId>
        <version>${postgresql.version}</version>
    </dependency>
    <dependency>
        <groupId>net.sf.ehcache</groupId>
        <artifactId>ehcache</artifactId>
        <version>${ehcache.version}</version>
    </dependency>
    <dependency>
        <groupId>com.google.code.gson</groupId>
        <artifactId>gson</artifactId>
        <version>${gson.version}</version>
    </dependency>
    <dependency>
        <groupId>redis.clients</groupId>
        <artifactId>jedis</artifactId>
        <version>${jedis.version}</version>
    </dependency>

in Main i have:

在主要我有:

LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
JoranConfigurator configurator = new JoranConfigurator();
configurator.setContext(loggerContext);
configurator.doConfigure(logbackFile)

it used for load logback.xml configuration outside of jar file

它用于在 jar 文件之外加载 logback.xml 配置

The funniest thing is that this program is working on local machine (Windows) (reading logback.xml, create file, write to file) but when i upload it to remote server (linux) i have strange error

最有趣的是,这个程序正在本地机器(Windows)上运行(读取 logback.xml,创建文件,写入文件)但是当我将它上传到远程服务器(linux)时我有奇怪的错误

Exception in thread "main" java.lang.ClassCastException: org.slf4j.impl.Log4jLoggerFactory cannot be cast to ch.qos.logback.classic.LoggerContext

the question is why he want to cast ch.qos.logback.classic.LoggerContext from org.slf4j.impl.Log4jLoggerFactory??

问题是他为什么要从 org.slf4j.impl 转换 ch.qos.logback.classic.LoggerContext。Log4jLoggerFactory??

I dont have anywhere any lib of log4j... i mean i dont have it on entire machine

我在任何地方都没有 log4j 的任何库...我的意思是我在整个机器上都没有它

one additional info: on Windows i have Java from Oracle and on Linux i have openjdk - it can be a problem?

一个附加信息:在 Windows 上我有来自 Oracle 的 Java,在 Linux 上我有 openjdk - 这可能是一个问题吗?

//=================dependency tree

//================== 依赖树

[INFO] +- org.apache.activemq:activemq-client:jar:5.8.0:compile
[INFO] |  +- org.apache.geronimo.specs:geronimo-jms_1.1_spec:jar:1.1.1:compile
[INFO] |  +- org.fusesource.hawtbuf:hawtbuf:jar:1.9:compile
[INFO] |  \- org.apache.geronimo.specs:geronimo-j2ee-management_1.1_spec:jar:1.0.1:compile
[INFO] +- com.mchange:c3p0:jar:0.9.2-pre6:compile
[INFO] |  \- com.mchange:mchange-commons-java:jar:0.2.3.1:compile
[INFO] +- commons-pool:commons-pool:jar:1.6:compile
[INFO] +- postgresql:postgresql:jar:9.1-901.jdbc4:compile
[INFO] +- net.sf.ehcache:ehcache:jar:2.9.0:compile
[INFO] +- com.google.code.gson:gson:jar:2.2.4:compile
[INFO] +- redis.clients:jedis:jar:2.1.0:compile
[INFO] +- org.slf4j:slf4j-api:jar:1.7.5:compile
[INFO] +- ch.qos.logback:logback-classic:jar:1.1.3:compile
[INFO] +- ch.qos.logback:logback-core:jar:1.1.3:compile
[INFO] \- junit:junit:jar:4.11:test
[INFO]    \- org.hamcrest:hamcrest-core:jar:1.3:test

回答by gursahib.singh.sahni

Due to an already existing dependency of SLF4J in your project or in some other inherited project, there might be conflicts during runtime. Adding an exclusion to my POMfile worked for me:

由于您的项目或其他一些继承项目中已经存在 SLF4J 的依赖关系,因此在运行时可能会发生冲突。向我的POM文件添加排除项对我有用:

    <exclusions>
        <exclusion>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
        </exclusion>
    </exclusions>

回答by legs

Based on what it says here: https://github.com/dropwizard/dropwizard/issues/1205, there is a race condition with initializing the libraries. I was having a similar problem where my program worked fine on NetBeans, but didn't work in a gnome terminal. A race condition explains the inconsistent behavior common in multiple cases. I don't fully understand why this happens, but there is a workaround here for a specific case: https://gist.github.com/mbknor/34944ea4589a5fc6974c. This example is from that link, it may not be applicable to the specific case mentioned above but can probably be adapted for specific cases fairly easily if you understand what's going on.

根据它在这里所说的:https://github.com/dropwizard/dropwizard/issues/1205,初始化库存在竞争条件。我遇到了类似的问题,我的程序在 NetBeans 上运行良好,但在 gnome 终端中不起作用。竞争条件解释了在多种情况下常见的不一致行为。我不完全理解为什么会发生这种情况,但这里有针对特定情况的解决方法:https: //gist.github.com/mbknor/34944ea4589a5fc6974c。此示例来自该链接,它可能不适用于上述特定情况,但如果您了解正在发生的事情,则可以很容易地针对特定情况进行调整。

static {
        System.out.println("Waiting for slf4j to finish initialization");
        while ( LoggerFactory.getILoggerFactory() instanceof SubstituteLoggerFactory){
            try {
                Thread.sleep(50);
            } catch (InterruptedException e) {
                break;
            }
        }
}

回答by Michael

Trying taking out the dependency

尝试消除依赖

<dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>1.7.7</version>
    </dependency>

This Dependency is included in

此依赖项包含在

<dependency>
        <groupId>ch.qos.logback</groupId>
        <artifactId>logback-classic</artifactId>
        <version>1.1.3</version>
    </dependency>

As you see in the logback-classic pom file - http://central.maven.org/maven2/ch/qos/logback/logback-classic/1.1.3/logback-classic-1.1.3.pom

正如你在 logback-classic pom 文件中看到的 - http://central.maven.org/maven2/ch/qos/logback/logback-classic/1.1.3/logback-classic-1.1.3.pom

Also make sure that when you initialize your logger that it is modeled after

还要确保当您初始化记录器时,它是根据

private static final Logger LOG = LoggerFactory
            .getLogger(ClassName.class)

回答by Fresh Codemonger

I had to switch the logger in my controller from:

我不得不将控制器中的记录器从以下位置切换:

private static final Logger log = LogbackUtil.getLogger( MyController.class );

to

private static final Logger log = LoggerFactory.getLogger( MyController.class );