logback中不同日志级别的不同日志文件

时间:2017-01-05 08:07:41

标签: java logback

我正在尝试使用以下Logback配置基于日志级别将日志信息插入到文件中。我想将日志信息插入每个不同的文件(如信息到信息文件,调试到调试文件..)实现这一点我使用下面的配置文件,我可以成功地将信息信息插入信息文件。但是我无法将调试信息插入到调试文件中。请帮助我解决这个问题。感谢。

<property name="DEV_HOME" value="d:/log" />

<appender name="FILE-INSERT-DEBUG"
    class="ch.qos.logback.core.rolling.RollingFileAppender">
    <filter class="ch.qos.logback.classic.filter.LevelFilter">
        <level>DEBUG</level>
        <onMismatch>DENY</onMismatch>
    </filter>     

    <file>${DEV_HOME}/debug.log</file>
    <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
        <Pattern>
            %d{yyyy-MM-dd HH:mm:ss} - %msg%n
        </Pattern>
    </encoder>

    <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
        <!-- rollover daily -->
        <fileNamePattern>${DEV_HOME}/archived/debug.%d{yyyy-MM-dd}.%i.log.zip
                    </fileNamePattern>
        <timeBasedFileNamingAndTriggeringPolicy
            class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
            <maxFileSize>10MB</maxFileSize>
        </timeBasedFileNamingAndTriggeringPolicy>
    </rollingPolicy>

</appender>

<appender name="FILE-INSERT-INFO"
    class="ch.qos.logback.core.rolling.RollingFileAppender">
    <filter class="ch.qos.logback.classic.filter.LevelFilter">
        <level>INFO</level>

    </filter>     

    <file>${DEV_HOME}/INFO.log</file>
    <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
        <Pattern>
            %d{yyyy-MM-dd HH:mm:ss} - %msg%n
        </Pattern>
    </encoder>

    <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
        <!-- rollover daily -->
        <fileNamePattern>${DEV_HOME}/archived/INFO.%d{yyyy-MM-dd}.%i.log.zip
                    </fileNamePattern>
        <timeBasedFileNamingAndTriggeringPolicy
            class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
            <maxFileSize>10MB</maxFileSize>
        </timeBasedFileNamingAndTriggeringPolicy>
    </rollingPolicy>

</appender>

<logger name="com.code" level="Debug"
    additivity="false">
    <appender-ref ref="FILE-INSERT-DEBUG" />
</logger> 

    <logger name="com.code" level="Info"
    additivity="false">
    <appender-ref ref="FILE-INSERT-INFO" />
</logger>  



    <root level="ERROR">
    <appender-ref ref="FILE-INSERT-DEBUG" />
    <appender-ref ref="FILE-INSERT-INFO" />
</root>

1 个答案:

答案 0 :(得分:1)

1)为什么没有打印调试日志?

  

发给具有有效等级的记录器的等级p的日志请求   q,如果p> = q,则启用。

它假设级别按如下顺序排列:TRACE&lt; DEBUG&lt;信息&lt;警告&lt; ERROR。

将根记录器级别设置为“DEBUG”,这是默认值。

2)更改您的logback配置,如下所示: -

<appender name="FILE-INSERT-DEBUG"
    class="ch.qos.logback.core.rolling.RollingFileAppender">
    <filter class="ch.qos.logback.classic.filter.LevelFilter">
        <level>DEBUG</level>
        <onMismatch>DENY</onMismatch>
    </filter>     

    <file>${DEV_HOME}/debug.log</file>
    <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
        <Pattern>
            %d{yyyy-MM-dd HH:mm:ss} - %msg%n
        </Pattern>
    </encoder>

    <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
        <!-- rollover daily -->
        <fileNamePattern>${DEV_HOME}/archived/debug.%d{yyyy-MM-dd}.%i.log.zip
                    </fileNamePattern>
        <timeBasedFileNamingAndTriggeringPolicy
            class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
            <maxFileSize>10MB</maxFileSize>
        </timeBasedFileNamingAndTriggeringPolicy>
    </rollingPolicy>

</appender>

<appender name="FILE-INSERT-INFO"
    class="ch.qos.logback.core.rolling.RollingFileAppender">
    <filter class="ch.qos.logback.classic.filter.LevelFilter">
      <level>INFO</level>
      <onMatch>ACCEPT</onMatch>
    </filter> 
     <filter class="ch.qos.logback.classic.filter.LevelFilter">
      <level>DEBUG</level>
      <onMatch>DENY</onMatch>
    </filter>     
    <file>${DEV_HOME}/INFO.log</file>
    <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
        <Pattern>
            %d{yyyy-MM-dd HH:mm:ss} - %msg%n
        </Pattern>
    </encoder>

    <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
        <!-- rollover daily -->
        <fileNamePattern>${DEV_HOME}/archived/INFO.%d{yyyy-MM-dd}.%i.log.zip
                    </fileNamePattern>
        <timeBasedFileNamingAndTriggeringPolicy
            class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
            <maxFileSize>10MB</maxFileSize>
        </timeBasedFileNamingAndTriggeringPolicy>
    </rollingPolicy>

</appender>


    <root level="DEBUG">
    <appender-ref ref="FILE-INSERT-DEBUG" />
    <appender-ref ref="FILE-INSERT-INFO" />
</root>

您可以轻松地将上述配置扩展到其他级别。