问题描述
在我的应用程序中,我使用SLF4j作为日志门面,并显示多个绑定
In my application, I'm using SLF4j as logging facade with multiple bindings present as
- slf4j-simple-1.6.2.jar
- logback-classic-1.0.4.jar
这两者在pom.xml中定义为
Both of these are defined in pom.xml as
**<!--MANDATORY -->**
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.6.2</version>
</dependency>
**<!--SIMPLY DUMPS THE MESSAGE TO ERROR STREAM -->**
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.6.2</version>
</dependency>
**<!--logback : further configuration in logback.xml -->**
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>1.0.4</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.0.4</version>
</dependency>
一个简单的测试程序,可以在本地运行
A simple test program, which can be run natively
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class TimePass {
private Logger log = LoggerFactory.getLogger(getClass());
public static void main(String[] args) {
new TimePass().run();
}
private void run() {
System.out.println(log);
log.info("WE ARE RUNNING");
System.out.println("HIHIHIHI");
}
}
此项目/webapp部署在2个位置.但是,当我同时在两者上运行时,结果却截然不同.
This project/webapp is deployed on 2 locations.But when I run it on both, the results are strikingly different.
On 1 instance, output is
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/home/hemant/.m2/repository/org/slf4j/slf4j-simple/1.6.2/slf4j-simple-1.6.2.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/home/hemant/.m2/repository/ch/qos/logback/logback-classic/1.0.4/logback-classic-1.0.4.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
0 [main] INFO com.expertly.bin.TimePass - WE ARE RUNNING
org.slf4j.impl.SimpleLogger(com.expertly.bin.TimePass)
HIHIHIHI
另一方面,它的
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/home/hemant/.m2/repository/ch/qos/logback/logback-classic/1.0.4/logback-classic-1.0.4.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/home/hemant/.m2/repository/org/slf4j/slf4j-simple/1.6.2/slf4j-simple-1.6.2.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
14:58:18,760 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback.groovy]
14:58:18,760 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback-test.xml]
14:58:18,760 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Found resource [logback.xml] at [file:/home/hemant/Project/Expertly/ee/workspace/branches/integration/target/ROOT/WEB-INF/classes/logback.xml]
14:58:18,823 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - debug attribute not set
14:58:18,827 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - About to instantiate appender of type [ch.qos.logback.classic.sift.SiftingAppender]
14:58:18,831 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - Naming appender as [SIFT]
14:58:18,853 |-INFO in ch.qos.logback.core.joran.action.NestedComplexPropertyIA - Assuming default type [ch.qos.logback.classic.sift.MDCBasedDiscriminator] for [discriminator] property
14:58:18,859 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - About to instantiate appender of type [ch.qos.logback.core.rolling.RollingFileAppender]
14:58:18,862 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - Naming appender as [FILE-HIBERNATE]
14:58:18,868 |-ERROR in ch.qos.logback.core.joran.spi.Interpreter@59:13 - no applicable action for [Encoding], current pattern is [[configuration][appender][Encoding]]
14:58:18,897 |-WARN in ch.qos.logback.core.rolling.RollingFileAppender[FILE-HIBERNATE] - This appender no longer admits a layout as a sub-component, set an encoder instead.
14:58:18,897 |-WARN in ch.qos.logback.core.rolling.RollingFileAppender[FILE-HIBERNATE] - To ensure compatibility, wrapping your layout in LayoutWrappingEncoder.
14:58:18,897 |-WARN in ch.qos.logback.core.rolling.RollingFileAppender[FILE-HIBERNATE] - See also http://logback.qos.ch/codes.html#layoutInsteadOfEncoder for details
14:58:18,905 |-INFO in ch.qos.logback.core.rolling.FixedWindowRollingPolicy@1fc6e42 - Will use zip compression
14:58:18,910 |-INFO in ch.qos.logback.core.rolling.RollingFileAppender[FILE-HIBERNATE] - Active log file name: catalina.base_IS_UNDEFINED/logs/hibernate.log
14:58:18,910 |-INFO in ch.qos.logback.core.rolling.RollingFileAppender[FILE-HIBERNATE] - File property is set to [catalina.base_IS_UNDEFINED/logs/hibernate.log]
14:58:18,911 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - About to instantiate appender of type [ch.qos.logback.classic.net.SMTPAppender]
14:58:18,918 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - Naming appender as [EMAIL]
14:58:18,937 |-INFO in ch.qos.logback.classic.joran.action.RootLoggerAction - Setting level of ROOT logger to ERROR
14:58:18,937 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [EMAIL] to Logger[ROOT]
14:58:18,937 |-INFO in ch.qos.logback.classic.joran.action.LoggerAction - Setting level of logger [org.hibernate.type] to INFO
14:58:18,937 |-INFO in ch.qos.logback.classic.joran.action.LoggerAction - Setting additivity of logger [org.hibernate.type] to false
14:58:18,937 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [FILE-HIBERNATE] to Logger[org.hibernate.type]
14:58:18,937 |-INFO in ch.qos.logback.classic.joran.action.LoggerAction - Setting level of logger [org.hibernate] to INFO
14:58:18,938 |-INFO in ch.qos.logback.classic.joran.action.LoggerAction - Setting additivity of logger [org.hibernate] to false
14:58:18,938 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [FILE-HIBERNATE] to Logger[org.hibernate]
14:58:18,938 |-INFO in ch.qos.logback.classic.joran.action.RootLoggerAction - Setting level of ROOT logger to DEBUG
14:58:18,938 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [SIFT] to Logger[ROOT]
14:58:18,938 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - End of configuration.
14:58:18,939 |-INFO in ch.qos.logback.classic.joran.JoranConfigurator@1aaf0b3 - Registering current configuration as safe fallback point
Logger[com.expertly.bin.TimePass]
HIHIHIHI
在这两种情况下似乎都存在问题,征用了多个绑定,但是顺序不同.
The issue seems to be in both the cases, multiple bindings were enlisted but in different order.
**在案例1中:它记录的位置:1. slf4j-simple-1.6.2.jar2. logback-classic-1.0.4.jar并且它使用了第一次绑定来记录
**In Case 1 : where it logged : 1. slf4j-simple-1.6.2.jar 2. logback-classic-1.0.4.jar And it used first binding to log
在情况2中:它没有记录日志,因此顺序相反,因此它去寻找logback.xml并且无法解析Web容器特定的变量($ {catalina.home})**
In Case 2 : where it didnt log, the order was opposite and hence it went for looking for logback.xml and couldn't resolve the webcontainer specific variables (${catalina.home})**
我不知道这种逆转的原因.
I don't know the reason for this reversal.
我需要两个绑定.以防万一,当我本机运行Java文件时,我希望slf4j-simple绑定优先于logback绑定.
I need both the bindings. Just in case when I run a java file natively, I want slf4j-simple binding to preceed the logback binding.
谢谢.
推荐答案
如在页面上所述错误消息中,这不是一个好主意.引用该页面上的注释:
As explained in the page pointed to in the error message, this is not a good idea. Quoting the note from that page:
这篇关于SLF4j解决多个绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!