John Han
John Han

Reputation: 33

logback included logger does not work

I use logback SLF4J as logging framework in a web application and I want to separate different logic module to different logback configuration file and included them in the central configuration file. so I got a logback-spring.xml and logback.xml (Central),the logback-spring.xml is:

<included>
   <logger name="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping" level="INFO" additivity="false">
     <appender-ref ref="REST" />
   </logger>

   <logger name="org.springframework" level="INFO" additivity="false">
    <appender-ref ref="REST" />
   </logger>
</included>

and logback.xml is:

<configuration scan="true" scanPeriod="30 seconds"> 
<contextName>TestSuite</contextName>

<property scope="context" resource="properties/logback.properties" />


<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">  
    <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">  
        <Pattern>%d{HH:mm:ss.SSS} %-5level %logger{80} [%file:%line] - %msg%n</Pattern>  
    </encoder>  
</appender>  

<appender name="REST" class="ch.qos.logback.core.rolling.RollingFileAppender">  
   <File>${LOG_HOME:-${java.io.tmpdir}}/RestApi.txt</File>
   <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">  
        <FileNamePattern>${LOG_HOME:-${java.io.tmpdir}}/%d{yyyy-MM/ww,aux}/RestApi.%d{yyyy-MM-dd}.log.gz</FileNamePattern>  
        <MaxHistory>30</MaxHistory>  
   </rollingPolicy>  
   <encoder>  
        <pattern>%date{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{80} - %msg%n</pattern>  
  </encoder>  
</appender>

<root level="INFO">
    <appender-ref ref="STDOUT" />
</root>  

<include optional="true" resource="logback-spring.xml" />

When application start, all logging events are written to the console. I can't figure out what's wrong with my configuration, has anybody encountered this problem?

Upvotes: 0

Views: 3549

Answers (2)

John Han
John Han

Reputation: 33

Actually we can register a status listener to inspect logback's internal status, so we can find out what's wrong in the configuration file.'Cause of logback just printing its internal status data on the console when warnings or errors occur during the parsing of the configuration file, we don't know what's going on when logback initialization otherwise.

<statusListener class="ch.qos.logback.core.status.OnConsoleStatusListener" />  

Logback configuration

Upvotes: 2

John Han
John Han

Reputation: 33

I finally figure out the problem. The logback.xml and logback-spring.xml located in same directory,but dose not in the root classpath directory, it's under the log direcotry, so when I change the include element's resource attribute to log/logback-spring.xml

<include optional="true" resource="log/logback-spring.xml" />

It's work perfectly!

Upvotes: 1

Related Questions