我们能否在appender级别设置ERROR和INFO级别?。
我正在使用logback.xml
我的logback.xml
<configuration>
<appender name="stdout" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%5p [%t] %m%n</pattern>
</encoder>
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>DEBUG</level>
</filter>
</appender>
<appender name="default-out" class="ch.qos.logback.core.rolling.RollingFileAppender"> <!-- Rolling file appender for rolling files -->
<param name="File" value="logs/app-track-log.log" />
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- daily rollover -->
<fileNamePattern>logs/app-track-log-%d{yyyy-MM-dd}.log</fileNamePattern>
<!-- keep 30 days' worth of history -->
<maxHistory>30</maxHistory>
</rollingPolicy>
<param name="encoding" value="UTF-8" />
<encoder>
<pattern>%-30([web] %d{HH:mm:ss.SSS} [%thread]) %-5level %logger{32} - %msg%n</pattern>
</encoder>
</appender>
<appender name="error-out" class="ch.qos.logback.core.rolling.RollingFileAppender"> <!-- Rolling file appender for rolling files -->
<param name="File" value="logs/error/app-error.log" />
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- daily rollover -->
<fileNamePattern>app-error.%d{yyyy-MM-dd}.log</fileNamePattern>
<!-- keep 30 days' worth of history -->
<maxHistory>30</maxHistory>
</rollingPolicy>
<param name="encoding" value="UTF-8" />
<encoder>
<pattern>%-30([web] %d{HH:mm:ss.SSS} [%thread]) %-5level %logger{32} - %msg%n</pattern>
</encoder>
</appender>
<!--here the logger to initiate the write operation -->
<logger name="org.company.controllers" level="ERROR" >
<appender-ref ref="error-out" />
</logger>
<logger name="org.company.controllers" level="INFO" >
<appender-ref ref="default-out" />
</logger>
<root level="OFF">
<appender-ref ref="default-out" />
</root>
</configuration>
我知道appender将执行写入部分(输出).logger会将其重定向到右侧的appender及其属性。
在我的情况下,我需要在不同的记录器级别(INFO / ERROR)中使用相同的包来提供不同文件中的数据。
我的目标是将ERROR数据写入一个文件,将INFO数据写入另一个文件。
答案 0 :(得分:0)
你需要的是appender中的levelfilter,为下面显示的错误输出appender配置级别过滤器ERROR
<appender name="error-out" class="ch.qos.logback.core.rolling.RollingFileAppender"> <!-- Rolling file appender for rolling files -->
<param name="File" value="logs/error/app-error.log" />
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- daily rollover -->
<fileNamePattern>app-error.%d{yyyy-MM-dd}.log</fileNamePattern>
<!-- keep 30 days' worth of history -->
<maxHistory>30</maxHistory>
</rollingPolicy>
<param name="encoding" value="UTF-8" />
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>ERROR</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
<encoder>
<pattern>%-30([web] %d{HH:mm:ss.SSS} [%thread]) %-5level %logger{32} - %msg%n</pattern>
</encoder>
</appender>
用于信息记录
<appender name="default-out" class="ch.qos.logback.core.rolling.RollingFileAppender"> <!-- Rolling file appender for rolling files -->
<param name="File" value="logs/app-track-log.log" />
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- daily rollover -->
<fileNamePattern>logs/app-track-log-%d{yyyy-MM-dd}.log</fileNamePattern>
<!-- keep 30 days' worth of history -->
<maxHistory>30</maxHistory>
</rollingPolicy>
<param name="encoding" value="UTF-8" />
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>INFO</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
<encoder>
<pattern>%-30([web] %d{HH:mm:ss.SSS} [%thread]) %-5level %logger{32} - %msg%n</pattern>
</encoder>
</appender>
我假设您正在寻找仅在两个单独的文件中记录信息和错误日志,否则您可能需要扩展过滤器。