所以我有 4 个 Maven 项目,我们称它们为 A、B、C 和 D。项目 A 依赖于项目 B 依赖于项目 C,项目 A 也依赖于项目 D。
D
^
|
A -> B -> C
项目A和B使用slf4j
项目 C 使用 java.util.logging
项目 D 使用 log4j
现在我真的不知道如何设置它的日志依赖项以使其正常工作。
我有的是:
项目 D 仅依赖 log4j
。
项目 C 不依赖任何东西,因为它只使用 java 的日志实用程序。
项目 B 依赖于 slf4j-api
以及 jul-to-slf4j
来记录 C 记录的内容
项目A依赖slf4j-api
、slf4j-log4j12
和log4j
项目 A 是我要运行的主程序。它有一个 log4j.properties 文件设置用于记录到控制台。
# Direct log messages to stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1} - %m%n
# Direct all messages there
log4j.rootLogger = INFO, stdout
现在的问题是,当我启动程序时,所有 slf4j 日志都将被忽略。我尝试在项目 A 中使用 log4j-Logger 记录一些东西,但它被记录下来了。
项目A的依赖树:
[INFO] --- maven-dependency-plugin:2.1:tree (default-cli) @ devopstool ---
[INFO] PROJECT A
[INFO] +- com.google.code.gson:gson:jar:2.3.1:compile
[INFO] +- org.antlr:antlr-runtime:jar:3.4:compile
[INFO] | +- org.antlr:stringtemplate:jar:3.2.1:compile
[INFO] | \- antlr:antlr:jar:2.7.7:compile
[INFO] +- org.tmatesoft.svnkit:svnkit:jar:1.8.9:compile
[INFO] | +- com.jcraft:jsch.agentproxy.svnkit-trilead-ssh2:jar:0.0.7:compile
[INFO] | | \- com.jcraft:jsch.agentproxy.core:jar:0.0.7:compile
[INFO] | +- net.java.dev.jna:jna-platform:jar:4.1.0:compile
[INFO] | +- net.java.dev.jna:jna:jar:4.1.0:compile
[INFO] | +- com.trilead:trilead-ssh2:jar:1.0.0-build217:compile
[INFO] | +- com.jcraft:jsch.agentproxy.connector-factory:jar:0.0.7:compile
[INFO] | | +- com.jcraft:jsch.agentproxy.usocket-jna:jar:0.0.7:compile
[INFO] | | | \- net.java.dev.jna:platform:jar:3.4.0:compile
[INFO] | | +- com.jcraft:jsch.agentproxy.usocket-nc:jar:0.0.7:compile
[INFO] | | +- com.jcraft:jsch.agentproxy.sshagent:jar:0.0.7:compile
[INFO] | | \- com.jcraft:jsch.agentproxy.pageant:jar:0.0.7:compile
[INFO] | +- de.regnis.q.sequence:sequence-library:jar:1.0.3:compile
[INFO] | \- org.tmatesoft.sqljet:sqljet:jar:1.1.10:compile
[INFO] +- org.tmatesoft.svnkit:svnkit-cli:jar:1.8.9:compile
[INFO] +- org.tmatesoft.svnkit:svnkit-javahl16:jar:1.8.9:compile
[INFO] | \- org.apache.subversion:svn-javahl-api:jar:1.8.1:compile
[INFO] +- com.jgoodies:forms:jar:1.2.1:compile
[INFO] +- org.jsoup:jsoup:jar:1.8.2:compile
[INFO] +- PROJECT D
[INFO] | +- com.google.guava:guava:jar:18.0:compile
[INFO] | +- javax.activation:activation:jar:1.1:compile
[INFO] | +- log4j:log4j:jar:1.2.17:compile
[INFO] | +- com.jcraft:jsch:jar:0.1.52:compile
[INFO] | \- org.jetbrains:annotations:jar:13.0:compile
[INFO] +- PROJECT B
[INFO] | +- org.apache.chemistry.opencmis:chemistry-opencmis-client-impl:jar:0.9.0:compile
[INFO] | | +- org.apache.chemistry.opencmis:chemistry-opencmis-client-api:jar:0.9.0:compile
[INFO] | | +- org.apache.chemistry.opencmis:chemistry-opencmis-commons-api:jar:0.9.0:compile
[INFO] | | +- org.apache.chemistry.opencmis:chemistry-opencmis-commons-impl:jar:0.9.0:compile
[INFO] | | | +- org.codehaus.woodstox:woodstox-core-asl:jar:4.2.0:compile
[INFO] | | | | +- javax.xml.stream:stax-api:jar:1.0-2:compile
[INFO] | | | | \- org.codehaus.woodstox:stax2-api:jar:3.1.1:compile
[INFO] | | | \- com.sun.xml.ws:jaxws-rt:jar:2.1.7:compile
[INFO] | | | +- javax.xml.ws:jaxws-api:jar:2.1:compile
[INFO] | | | +- com.sun.xml.stream.buffer:streambuffer:jar:0.9:compile
[INFO] | | | +- com.sun.org.apache.xml.internal:resolver:jar:20050927:compile
[INFO] | | | \- org.jvnet:mimepull:jar:1.3:compile
[INFO] | | +- org.apache.chemistry.opencmis:chemistry-opencmis-client-bindings:jar:0.9.0:compile
[INFO] | | \- org.apache.felix:org.osgi.core:jar:1.0.0:compile
[INFO] | +- org.alfresco.cmis.client:alfresco-opencmis-extension:jar:0.3:compile
[INFO] | +- org.slf4j:slf4j-api:jar:1.7.7:compile
[INFO] | +- commons-configuration:commons-configuration:jar:1.10:compile
[INFO] | | +- commons-lang:commons-lang:jar:2.6:compile
[INFO] | | \- commons-logging:commons-logging:jar:1.1.1:compile
[INFO] | +- PROJECT C
[INFO] | | +- javax.xml.bind:jaxb-api:jar:2.2.7:compile
[INFO] | | +- com.sun.xml.bind:jaxb-impl:jar:2.2.7:compile
[INFO] | | | +- com.sun.xml.bind:jaxb-core:jar:2.2.7:compile
[INFO] | | | | \- com.sun.istack:istack-commons-runtime:jar:2.16:compile
[INFO] | | | \- com.sun.xml.fastinfoset:FastInfoset:jar:1.2.12:compile
[INFO] | | | \- javax.xml.bind:jsr173_api:jar:1.0:compile
[INFO] | | +- com.sun.xml.messaging.saaj:saaj-impl:jar:1.3.23:compile
[INFO] | | | +- javax.xml.soap:javax.xml.soap-api:jar:1.3.5:compile
[INFO] | | | +- org.jvnet.mimepull:mimepull:jar:1.9:compile
[INFO] | | | \- org.jvnet.staxex:stax-ex:jar:1.7.4:compile
[INFO] | | \- commons-codec:commons-codec:jar:1.4:compile
[INFO] | +- commons-cli:commons-cli:jar:1.2:compile
[INFO] | \- org.slf4j:jul-to-slf4j:jar:1.7.7:compile
[INFO] +- com.miglayout:miglayout-swing:jar:5.0:compile
[INFO] | \- com.miglayout:miglayout-core:jar:5.0:compile
[INFO] \- org.slf4j:slf4j-log4j12:jar:1.7.7:compile
最佳答案
根据您的依赖树,您有多个 SLF4J 绑定(bind)。
[INFO] | | | | \- org.slf4j:slf4j-nop:jar:1.5.3:runtime
[INFO] | | | +- org.slf4j:slf4j-jdk14:jar:1.5.6:runtime
[INFO] \- org.slf4j:slf4j-log4j12:jar:1.7.7:compile
如果是这种情况,您会在标准错误输出中找到与此类似的消息(请注意,这可能与标准输出不同,您实际上并没有看到 - 这取决于您执行应用程序的方式)。
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/home/frantisek/.m2/repository/org/slf4j/slf4j-nop/1.7.12/slf4j-nop-1.7.12.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/home/frantisek/.m2/repository/ch/qos/logback/logback-classic/1.1.3/logback-classic-1.1.3.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Actual binding is of type [org.slf4j.helpers.NOPLoggerFactory]
实际上,整个 maven-assembly-plugin 依赖关系似乎并不正确,除非您正在开发 maven 插件。
[INFO] | | \- org.apache.maven.plugins:maven-assembly-plugin:jar:2.5.3:compile
要解决此问题,请排除 slf4j-nop
和 slf4j-jdk14
或删除整个依赖项。
关于java - 如何使用 slf4j 和不同的日志实用程序设置 4 个 Maven 项目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32113857/