gpt4 book ai didi

java - 由于 log4j,无法使用 logback

转载 作者:搜寻专家 更新时间:2023-11-01 03:21:25 25 4
gpt4 key购买 nike

你好,我有 logback 和 slf4j 的问题,我正在编写一个简单的应用程序,稍后将其打包在 jar 中,我想在那里添加使用 logback 的日志记录

我正在使用:

    <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>

在主要我有:

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

用于加载jar文件外的logback.xml配置

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

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

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

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

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

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

[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

最佳答案

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

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

关于java - 由于 log4j,无法使用 logback,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29847047/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com