参考文章:
https://www.cnblogs.com/warking/p/5710303.html
下面是自己项目中的配置,做个记录
<!-- 日志相关包 -->
<!-- Slf4j log interface -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j-version}</version>
</dependency>
<!-- commons-logging translate to slf4j -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>${slf4j-version}</version>
</dependency>
<!-- User logback as log output actuator -->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>${logback-version}</version>
</dependency>
<configuration>
<!-- 获取时间戳字符串并格式化 -->
<timestamp key="LOG_TIME" datePattern="yyyy-MM-dd"/>
<!-- property 用于定义变量值 通过${name} 可以用于上下文中 -->
<property name="WEB_NAME" value="Xp02" />
<property name="LOG_PATH" value="日志文件输出路径" />
<property name="MAX_HISTORY" value="30"/>
<property name="MAX_SIZE" value="100MB"/>
<property name="LOG_PATTERN" value="[%d{yyyy-MM-dd HH:mm:ss.SSS}][%thread][%level][%logger][%L]%msg%n" />
<!-- 设置上下文名称 -->
<contextName>${WEB_NAME}</contextName>
<!-- ConsoleAppender 输出日志到控制台 -->
<!-- encoder 对日志进行格式化 -->
<!-- target 字符串system.out或者system.err -->
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<encoder charset="UTF-8">
<pattern>${LOG_PATTERN}</pattern>
</encoder>
<target>System.out</target>
</appender>
<!-- appender 写日志的组件 -->
<!-- LevelFilter 级别过滤器 -->
<!-- onMatch 符合过滤条件 -->
<!-- onMismatch 不符合过滤条件 -->
<!-- RollingFileAppender 滚动记录日志文件 -->
<!-- file 文件名 -->
<!-- true 追加到文件结尾, false 清空文件内容 -->
<!-- TimeBasedRollingPolicy 常用的果冻策略,根据时间定制滚动策略 -->
<!-- fileNamePattern 必要节点, 包含文件名和时间,以及制定时间格式 -->
<!-- maxHistory 可选节点,控制保留的文件最大数量,超出就删除旧文件 -->
<!-- maxFileSize 设置文件的大小 -->
<!-- rollingPolicy 日志分包策略 -->
<!-- TimeBasedRollingPolicy 基于时间的分包策略 -->
<!-- fileNamePattern 根据此节点设置的时间进行分包 -->
<appender name="SYS_INFO" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOG_PATH}/system-info.log</file>
<append>true</append>
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>INFO</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${LOG_PATH}/%d{yyyy-MM-dd}/system-info.%d{yyyy-MM-dd}.%i.log.gz</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>${MAX_SIZE}</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
<maxHistory>${MAX_HISTORY}</maxHistory>
</rollingPolicy>
<encoder charset="UTF-8">
<pattern>${LOG_PATTERN}</pattern>
</encoder>
</appender>
<appender name="SYS_ERROR" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOG_PATH}/system-error.log</file>
<append>true</append>
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>ERROR</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${LOG_PATH}/%d{yyyy-MM-dd}/system-error.%d{yyyy-MM-dd}.%i.log.gz</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>${MAX_SIZE}</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
<maxHistory>${MAX_HISTORY}</maxHistory>
</rollingPolicy>
<encoder charset="UTF-8">
<pattern>${LOG_PATTERN}</pattern>
</encoder>
</appender>
<appender name="WEB_INFO" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOG_PATH}/web-info.log</file>
<append>true</append>
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>INFO</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${LOG_PATH}/%d{yyyy-MM-dd}/app-info.%d{yyyy-MM-dd}.%i.log.gz</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>${MAX_SIZE}</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
<maxHistory>${MAX_HISTORY}</maxHistory>
</rollingPolicy>
<encoder charset="UTF-8">
<pattern>${LOG_PATTERN}</pattern>
</encoder>
</appender>
<appender name="WEB_ERROR" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOG_PATH}/web-error.log</file>
<append>true</append>
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>ERROR</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${LOG_PATH}/%d{yyyy-MM-dd}/app-error.%d{yyyy-MM-dd}.%i.log.gz</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>${MAX_SIZE}</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
<maxHistory>${MAX_HISTORY}</maxHistory>
</rollingPolicy>
<encoder charset="UTF-8">
<pattern>${LOG_PATTERN}</pattern>
</encoder>
</appender>
<!-- logger 用来设置某一个包或具体的某一个类的日志打印级别 -->
<!-- name 用来指定受此loger约束的某一个包或者具体的某一个类 -->
<!-- level 设置打印级别 -->
<logger name="包名" level="INFO" additivity="false">
<appender-ref ref="WEB_INFO"/>
<appender-ref ref="WEB_ERROR"/>
</logger>
<root level="INFO">
<appender-ref ref="SYS_INFO"/>
<appender-ref ref="SYS_ERROR"/>
</root>
</configuration>