1

I need to log info and error in separate files . I tried the below configuration. Though I got logs for CACHE-AUDIT and SERVICE_AUDIT, nothing gets printed for info and error log. Am i missing some configuration here.

logback.xml

<property name="LOG_HOME" value="E:\\logs"/>

<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
    <layout class="ch.qos.logback.classic.PatternLayout">
        <Pattern>
            %d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n
        </Pattern>
    </layout>
</appender>

<appender name="CACHE-AUDIT" class="ch.qos.logback.core.rolling.RollingFileAppender">
    <file>${LOG_HOME}/cache-service.log</file>
    <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
        <fileNamePattern>cache-service.%i.log.zip</fileNamePattern>
        <minIndex>1</minIndex>
        <maxIndex>7</maxIndex>
    </rollingPolicy>

    <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
        <maxFileSize>10MB</maxFileSize>
    </triggeringPolicy>
    <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
        <Pattern>
            %d{yyyy-MM-dd HH:mm:ss} - %msg%n
        </Pattern>
    </encoder>
</appender>

<appender name="FILE-ERROR"
          class="ch.qos.logback.core.rolling.RollingFileAppender">
    <file>${LOG_HOME}/error.log</file>
    <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
        <level>ERROR</level>
    </filter>
    <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
        <fileNamePattern>error.%i.log.zip</fileNamePattern>
        <minIndex>1</minIndex>
        <maxIndex>7</maxIndex>
    </rollingPolicy>

    <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
        <maxFileSize>10MB</maxFileSize>
    </triggeringPolicy>
    <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
        <Pattern>
            %d{yyyy-MM-dd HH:mm:ss} - %msg%n
        </Pattern>
    </encoder>
</appender>

<appender name="FILE-INFO"
          class="ch.qos.logback.core.rolling.RollingFileAppender">
    <file>${LOG_HOME}/info.log</file>
    <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
        <level>INFO</level>
    </filter>
    <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
        <fileNamePattern>info.%i.log.zip</fileNamePattern>
        <minIndex>1</minIndex>
        <maxIndex>5</maxIndex>
    </rollingPolicy>

    <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
        <maxFileSize>10MB</maxFileSize>
    </triggeringPolicy>
    <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
        <Pattern>
            %d{yyyy-MM-dd HH:mm:ss} - %msg%n
        </Pattern>
    </encoder>
</appender>

<appender name="SERVICE-AUDIT"
          class="ch.qos.logback.core.rolling.RollingFileAppender">
    <file>${LOG_HOME}/service-audit.log</file>
    <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
        <fileNamePattern>service-audit.%i.log.zip</fileNamePattern>
        <minIndex>1</minIndex>
        <maxIndex>5</maxIndex>
    </rollingPolicy>

    <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
        <maxFileSize>10MB</maxFileSize>
    </triggeringPolicy>
    <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
        <Pattern>
            %d{yyyy-MM-dd HH:mm:ss} - %msg%n
        </Pattern>
    </encoder>
</appender>

<logger name="ch.base.cache" level="debug" additivity="false">
    <appender-ref ref="CACHE-AUDIT"/>
</logger>

<logger name="ch.base.service.client" level="debug" additivity="false">
    <appender-ref ref="SERVICE-AUDIT"/>
</logger>

<logger name="ch.base" level="info">
    <appender-ref ref="FILE-INFO"/>
</logger>

<logger name="ch.base" level="error">
    <appender-ref ref="FILE-ERROR"/>
</logger>

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

Tiny
  • 683
  • 5
  • 18
  • 36

1 Answers1

0

You have two loggers with the same name. The one with level="error" would certainly keep the other one from getting anything below ERROR. Although I'm not quite sure why you don't see any error logging either.

Try merge these two blocks:

<logger name="ch.base" level="info">
    <appender-ref ref="FILE-INFO"/>
</logger>

<logger name="ch.base" level="error">
    <appender-ref ref="FILE-ERROR"/>
</logger>

into:

<logger name="ch.base" level="info">
    <appender-ref ref="FILE-INFO"/>
    <appender-ref ref="FILE-ERROR"/>
</logger>
jingx
  • 3,698
  • 3
  • 24
  • 40
  • I changed the setting as your described .. now info and error is getting printed in both the files .. can i get info and error in separate files .. – Tiny Jun 14 '17 at 08:52
  • Are you sure? The info log would have both INFO and ERROR, but the error log should only have ERROR due to the threshold filter. Anyway, if you want exactly INFO in the info log, try `LevelFilter` instead of `ThresholdFilter`. – jingx Jun 14 '17 at 14:08
  • I have removed the ThresholdFilter.. only level is used .. Then too the same logs are printed in both the files .. there is no separation done based on level filter .. – Tiny Jun 14 '17 at 14:21
  • Do you mean level filter to the appender .. .. – Tiny Jun 14 '17 at 14:33
  • You might need `LevelFilter` for both appenders, each set to the desired level. Unfortunately I'm not with an environment allowing me to experiment at the moment. See the documentation: https://logback.qos.ch/manual/filters.html – jingx Jun 14 '17 at 14:38