logback怎么写?分类输出日志到不同的文件

此appender有顺序,最好不要乱调顺序,输出日志如下:

drwxr-xr-x 2 root root 4096 Dec 3 00:00 2019-12-02
drwxr-xr-x 2 root root 4096 Dec 4 13:48 2019-12-03
drwxr-xr-x 2 root root 4096 Dec 5 09:55 2019-12-04

-rw-r--r-- 1 root root 29712 Dec 5 10:16 error.log
-rw-r--r-- 1 root root 426 Dec 5 10:28 framework.log
-rw-r--r-- 1 root root 237 Dec 5 10:28 XXXXMo.log



<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<include resource="org/springframework/boot/logging/logback/defaults.xml"/>
<property name="CONSOLE_LOG_PATTERN" value="${CONSOLE_LOG_PATTERN:-%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} %clr(${LOG_LEVEL_PATTERN:-%5p}) %clr(${PID:- }){magenta} %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}}"/>
<property name="FILE_LOG_PATTERN" value="${FILE_LOG_PATTERN:-%d{yyyy-MM-dd HH:mm:ss.SSS} ${LOG_LEVEL_PATTERN:-%5p} ${PID:- } --- [%t] %-40.40logger{39} : %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}}"/>
<property name="LOG_FILE" value="../logs/service-XXXXMo"/>
<property name = "log_level" value = "INFO"/>
<property name = "max_log_files" value = "20"/>

<!--控制台-->
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>${CONSOLE_LOG_PATTERN}</pattern>
<charset>utf8</charset>
</encoder>

<!--这段注释掉,测试是通过的,过滤掉带nacos关键字的日志不打印 <filter class="ch.qos.logback.core.filter.EvaluatorFilter">
<onMatch>DENY</onMatch>
<onMismatch>ACCEPT</onMismatch>

<evaluator>
<expression>
return message.contains("nacos");
</expression>
</evaluator>
</filter>-->
</appender>

<!-- 错误日志(框架 + 业务) -->
<appender name="ERROR" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOG_FILE}/error.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- rollover daily -->
<fileNamePattern>${LOG_FILE}/%d{yyyy-MM-dd}/error.log</fileNamePattern>
<maxHistory>${max_log_files}</maxHistory>
</rollingPolicy>

<encoder>
<pattern>${FILE_LOG_PATTERN}</pattern>
</encoder>

<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>ERROR</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
</appender>

<!--业务日志文件-->
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<encoder>
<pattern>${FILE_LOG_PATTERN}</pattern>
</encoder>
<file>${LOG_FILE}/XXXXMo.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${LOG_FILE}/%d{yyyy-MM-dd}/XXXXMo.log</fileNamePattern>
<maxHistory>${max_log_files}</maxHistory>
</rollingPolicy>

<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>INFO</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>

</appender>

<!-- 框架日志 -->
<appender name="FRAMEWORK" class="ch.qos.logback.core.rolling.RollingFileAppender">

<encoder>
<pattern>${FILE_LOG_PATTERN}</pattern>
</encoder>

<file>${LOG_FILE}/framework.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- rollover daily -->
<fileNamePattern>${LOG_FILE}/%d{yyyy-MM-dd}/framework.log</fileNamePattern>
<maxHistory>${max_log_files}</maxHistory>

</rollingPolicy>

<filter class="ch.qos.logback.core.filter.EvaluatorFilter">
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>

<evaluator>
<expression>
event.getLevel() == ch.qos.logback.classic.Level.${log_level}
|| event.getLevel() == ch.qos.logback.classic.Level.WARN
</expression>
</evaluator>
</filter>
</appender>

<logger name="org.apache.ibatis" level="${log_level}" additivity="false">
<appender-ref ref="FRAMEWORK"/>
<appender-ref ref="ERROR"/>
</logger>

<logger name="org.springframework" level="${log_level}" additivity="false">
<appender-ref ref="FRAMEWORK"/>
<appender-ref ref="ERROR"/>
</logger>

<logger name="com.alibaba" level="${log_level}" additivity="false">
<appender-ref ref="FRAMEWORK"/>
<appender-ref ref="ERROR"/>
</logger>

<logger name="org.apache.zookeeper" level="${log_level}" additivity="false">
<appender-ref ref="FRAMEWORK"/>
<appender-ref ref="ERROR"/>
</logger>

<logger name="org.I0Itec" level="${log_level}" additivity="false">
<appender-ref ref="FRAMEWORK"/>
<appender-ref ref="ERROR"/>
</logger>

<logger name="org.quartz" level="${log_level}" additivity="false">
<appender-ref ref="FRAMEWORK"/>
<appender-ref ref="ERROR"/>
</logger>

<logger name="org.hibernate" level="${log_level}" additivity="false">
<appender-ref ref="FRAMEWORK"/>
<appender-ref ref="ERROR"/>
</logger>

<logger name="org.mybatis" level="${log_level}" additivity="false">
<appender-ref ref="FRAMEWORK"/>
<appender-ref ref="ERROR"/>
</logger>

<logger name="com.mysql" level="${log_level}" additivity="false">
<appender-ref ref="FRAMEWORK"/>
<appender-ref ref="ERROR"/>
</logger>

<logger name="com.alibaba.nacos" level="${log_level}" additivity="false">
<appender-ref ref="FRAMEWORK"/>
<appender-ref ref="ERROR"/>
</logger>




<root level="info">
<appender-ref ref="FILE" />
<appender-ref ref="ERROR" />
<appender-ref ref="CONSOLE" />
</root>


</configuration>
posted @ 2019-12-05 14:23  仔儿仔  阅读(1369)  评论(0编辑  收藏  举报