javaslf4jlogback

Logback configuration file -- write all except debug to file, as logging to console


I am trying to configure logback and slf4j to my eclipse. However, I am a bit confused about configuration file. I just want log output should be written to both cosole and file, but only debug information should be closed to be written to file. As a result, I have wrote some configuration file but it sucks, not even working. Anyway, how can I achieve this wish?

<configuration>

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

  <logger name="com.lordofthejars.foo" level="INFO" additivity="false">
   <appender-ref ref="STDOUT" />
  </logger>

  <root level="DEBUG">          
    <appender-ref ref="STDOUT" />
  </root>  
  
</configuration>

Solution

  • You should set level filter for fileappender to DEBUG

    <configuration>
    
         <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
            <encoder>
                <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{5} - %msg%n</pattern>
            </encoder>
         </appender>
    
         <appender name="fileAppender1" class="ch.qos.logback.core.FileAppender">
           <filter class="ch.qos.logback.classic.filter.LevelFilter">
                <level>DEBUG</level>
                <onMatch>ACCEPT</onMatch>
                <onMismatch>DENY</onMismatch>
            </filter>
            <file>c:/logs/kplogback.log</file>
            <append>true</append>
            <encoder>
                <pattern>%d [%thread] %-5level %logger{35} - %msg%n</pattern>
            </encoder>
        </appender>
    
        <root level="DEBUG">
            <appender-ref ref="STDOUT" />
            <appender-ref ref="fileAppender1" />
        </root>
    
    </configuration>