SLF4j解析多个绑定

时间:2014-10-07 09:36:32

标签: java maven slf4j logback

在我的应用程序中,我使用SLF4j作为日志外观,其中多个绑定显示为

  1. slf4j-simple-1.6.2.jar
  2. 的logback-经典1.0.4.jar
  3. 这两个都在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 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
    

    这两个案例似乎都存在问题,多个绑定都以不同的顺序登记。

    **案例1:记录的地方:  1. slf4j-simple-1.6.2.jar  2. logback-classic-1.0.4.jar  它使用第一个绑定来记录

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

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

    我需要两个绑定。如果我本地运行java文件,我希望slf4j-simple绑定优先于logback绑定。

    提前致谢。

1 个答案:

答案 0 :(得分:2)

page pointed to in the error message中所述,这不是一个好主意。从该页面引用说明:

  

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