2016-05-12 104 views
0

我正在一個Spring-MVC應用程序中嘗試再次進行日誌記錄。不幸的是,有時候它剛剛停止工作,我不知道是什麼原因造成的。我在網上嘗試了一些建議,但沒有用。有什麼建議麼?春天:日誌記錄不與log4j或logback一起工作

的pom.xml:

<packaging>war</packaging> 
    <properties> 
     <java-version>1.8</java-version> 
     <org.springframework-version>4.1.6.RELEASE</org.springframework-version> 
     <org.aspectj-version>1.7.4</org.aspectj-version> 
     <org.slf4j-version>1.7.5</org.slf4j-version> 
     <hibernate.version>4.3.9.Final</hibernate.version> 
     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 
     <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> 
     <springsecurity.version>4.0.1.RELEASE</springsecurity.version> 
     <spring-platform.version>1.1.3.RELEASE</spring-platform.version> 
     <jetty.version>9.2.9.v20150224</jetty.version> 
    </properties> 

    <parent> 
     <groupId>io.spring.platform</groupId> 
     <artifactId>platform-bom</artifactId> 
     <version>1.1.3.RELEASE</version> 
     <relativePath /> 
    </parent> 

    <dependencies> 

    <!-- Spring framework dependencies --> 

     <dependency> 
      <groupId>org.springframework.mobile</groupId> 
      <artifactId>spring-mobile-device</artifactId> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework.data</groupId> 
      <artifactId>spring-data-redis</artifactId> 
     </dependency> 

     <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-webmvc</artifactId> 
     </dependency> 

     <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-orm</artifactId> 
     </dependency> 

     <!-- Spring security dependencies --> 
     <dependency> 
      <groupId>org.springframework.security</groupId> 
      <artifactId>spring-security-config</artifactId> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework.security</groupId> 
      <artifactId>spring-security-taglibs</artifactId> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework.security</groupId> 
      <artifactId>spring-security-web</artifactId> 
     </dependency> 


    <!-- Logging --> 
     <dependency> 
      <groupId>org.slf4j</groupId> 
      <artifactId>slf4j-api</artifactId> 
      <version>1.7.5</version> 
     </dependency> 
     <dependency> 
      <groupId>org.slf4j</groupId> 
      <artifactId>jcl-over-slf4j</artifactId> 
      <version>1.7.5</version> 
      <scope>runtime</scope> 
     </dependency> 
     <dependency> 
      <groupId>ch.qos.logback</groupId> 
      <artifactId>logback-classic</artifactId> 
      <version>1.0.13</version> 
      <scope>runtime</scope> 
     </dependency> 
     <dependency> 
      <groupId>ch.qos.logback</groupId> 
      <artifactId>logback-core</artifactId> 
      <version>1.0.13</version> 
      <scope>runtime</scope> 
     </dependency> 

     <dependency> 
      <groupId>log4j</groupId> 
      <artifactId>apache-log4j-extras</artifactId> 
      <version>1.1</version> 
     </dependency> 

我都和的log4j.xml logback.xml:

的log4j.xml:

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE log4j:configuration SYSTEM "http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/xml/doc-files/log4j.dtd"> 
<log4j:configuration> 


    <!-- Appenders --> 
    <appender name="console" class="org.apache.log4j.ConsoleAppender"> 
     <param name="Target" value="System.out" /> 
     <layout class="org.apache.log4j.PatternLayout"> 
      <param name="ConversionPattern" value="%-5p: %c - %m%n" /> 
     </layout> 
    </appender> 
    <logger name="org.cometd"> 
     <level value="debug"/> 
    </logger> 
    <!-- Root Logger --> 
    <root> 
     <priority value="OFF" /> 
     <appender-ref ref="console" /> 
    </root> 

</log4j:configuration> 

logback.xml:

<?xml version="1.0" encoding="UTF-8"?> 
<configuration> 
    <include resource="org/springframework/boot/logging/logback/base.xml"/> 


    <logger name="org.cometd" level="debug"/> 
    <logger name="com.tooltank.spring.chat.ChatServiceImpl" level="info"/> 
</configuration> 

服務器啓動期間,我得到這個:

SLF4J: Class path contains multiple SLF4J bindings. 
SLF4J: Found binding in [jar:file:/home/logback-classic-1.0.13.jar!/org/slf4j/impl/StaticLoggerBinder.class] 
SLF4J: Found binding in [jar:file:/home//WEB-INF/lib/slf4j-log4j12-1.7.12.jar!/org/slf4j/impl/StaticLoggerBinder.class] 
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation. 
13:43:29,173 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback.groovy] 
13:43:29,173 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback-test.xml] 
13:43:29,174 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Found resource [logback.xml] at [file:/home/path/to/logback.xml] 
13:43:29,292 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - debug attribute not set 
13:43:29,298 |-ERROR in ch.qos.logback.core.joran.action.IncludeAction - Could not find resource corresponding to [org/springframework/boot/logging/logback/base.xml] 
SLF4J: Actual binding is of type [ch.qos.logback.classic.util.ContextSelectorStaticBinder] 
13:43:29,299 |-INFO in ch.qos.logback.classic.joran.action.LoggerAction - Setting level of logger [org.cometd] to DEBUG 
13:43:29,299 |-INFO in ch.qos.logback.classic.joran.action.LoggerAction - Setting level of logger [com.tooltank.spring.chat.ChatServiceImpl] to INFO 
13:43:29,299 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - End of configuration. 
13:43:29,300 |-INFO in [email protected] - Registering current configuration as safe fallback point 

我錯過了什麼?謝謝。

回答

1

您應該避免在一個應用程序中使用log4j和logback。如果你在你的classpath既瓶類裝入器將選擇其中的任何一個(種隨機..) 被此日誌聲明表示:

SLF4J: Class path contains multiple SLF4J bindings. 

如果你打算使用的logback你需要把logback.xml文件放在應用程序的類路徑中。從文檔:

如果在類路徑中找到logback,它將嘗試使用文件logback-test.xml或logback.xml進行自我配置。

您也可以看看這個鏈接:https://docs.spring.io/spring-boot/docs/current/reference/html/howto-logging.html#howto-configure-logback-for-logging

編輯: 我想你還需要一個附加目的地添加到控制檯,例如:

<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> 
<!-- encoders are assigned the type 
    ch.qos.logback.classic.encoder.PatternLayoutEncoder by default --> 
<encoder> 
    <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern> 
</encoder> 

,然後將appender添加到您的記錄器中:

<logger name="org.cometd" additivity="false"> 
    <level value="DEBUG" /> 
    <appender-ref ref="STDOUT" /> 
</logger> 

此致敬禮!

+0

看目前的情況,你會推薦什麼,刪除log4j或logback,哪一個更容易管理,我可以通過調試等從庫中獲取日誌。 –

+0

我個人最喜歡的是保持logback,但這取決於你需要什麼。查看我在答案中添加的鏈接。我想這可以幫助你 – Joschi

+0

嗨,我已經刪除了log4j和slf4j的所有依賴關係,並且只保留了ch.qos.logback中的logback-classic和logback-core。 xml的配置與我在鏈接中給出的相同。即使在那之後,我看不到調試消息,任何想法。啓動日誌:http://pastebin.com/pSdmY77U –