我需要在单独的文件中记录信息和错误。我尝试了以下配置。虽然我得到了CACHE-AUDIT和SERVICE_AUDIT的日志,但是没有打印任何信息和错误日志。我在这里错过了一些配置。
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>
答案 0 :(得分:0)
您有两个具有相同名称的记录器。 level="error"
的那个肯定会让另一个人得不到任何低于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>