2014-10-07 307 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將命名它實際綁定的框架/實現類。