2014-10-07 134 views
0

在我的应用程序,我使用SLF4J与存在SLF4J解决多个绑定

  1. SLF4J-简单1.6.2.jar多个绑定登录门面
  2. 的logback经典-1.0.4。 JAR

这两种在pom.xml中被定义为

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

一个简单的测试程序,该程序可以被本地运行

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个位置。 但是,当我在两者上运行它时,结果都显着不同。

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 [email protected]: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 [email protected] - 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 [email protected] - Registering current configuration as safe fallback point 

Logger[com.expertly.bin.TimePass] 
HIHIHIHI 

这个问题似乎是在这两种情况下,多个绑定被征了,但在不同的顺序。

**在情况1中:在那里记录: 1. SLF4J-简单1.6.2.jar 2.的logback-经典1.0.4.jar 它所用第一结合登录

在案例2中:它没有登录,订单相反,因此它寻找logback.xml并且无法解析webcontainer特定变量($ {catalina.home})**

我不知道这种逆转的原因。

我需要这两个绑定。在我本身运行java文件的情况下,我希望slf4j-简单绑定在logback绑定之前。

在此先感谢。

回答

2

正如page pointed to in the error message中所述,这不是一个好主意。引用该页面的注释:

SLF4J发出的警告就是这样一个警告。即使存在多个绑定,SLF4J也会选择一个日志框架/实现并绑定它。 SLF4J选择绑定的方式由JVM确定,并且对于所有实际目的应视为随机。从版本1.6.6开始,SLF4J将命名它实际绑定的框架/实现类。