我需要在单独的文件中记录信息和错误。我尝试了以下配置。虽然我得到了 CACHE-AUDIT 和 SERVICE_AUDIT 的日志,但没有打印任何信息和错误日志。我在这里缺少一些配置。
登录.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>
您有两个同名的记录器。带有level="error"
的那个肯定会阻止另一个得到低于错误的任何内容。虽然我也不太确定为什么你也没有看到任何错误日志记录。
尝试合并这两个块:
<logger name="ch.base" level="info">
<appender-ref ref="FILE-INFO"/>
</logger>
<logger name="ch.base" level="error">
<appender-ref ref="FILE-ERROR"/>
</logger>
到:
<logger name="ch.base" level="info">
<appender-ref ref="FILE-INFO"/>
<appender-ref ref="FILE-ERROR"/>
</logger>