logback-spring.xml配置
<?xml version="1.0" encoding="UTF-8"?>
<configuration debug="false">
<contextName>vhsc-api4</contextName>
<conversionRule conversionWord="token" converterClass="com.viewhigh.vhsc.platform.log.LogThreadTokenConvert" />
<conversionRule conversionWord="clr" converterClass="org.springframework.boot.logging.logback.ColorConverter" />
<!-- 控制台输出 -->
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%clr([%token]) %d{YY-MM-dd HH:mm:ss.SSS} %clr(%-5level) %logger{32}[%line] - %clr(%msg) %n</pattern>
</encoder>
</appender>
<!-- 输出到文件 -->
<appender name="DEBUG_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>DEBUG</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
<file>${LOG_PATH}/debug.log</file>
<!-- 设置滚动策略 -->
<!-- 按天来回滚,如果需要按小时来回滚,则设置为{yyyy-MM-dd_HH} -->
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!--设置日志命名模式 -->
<fileNamePattern>${LOG_PATH}/debug.log.%d{yyyy-MM-dd}.%i.log</fileNamePattern>
<!-- 如果按天来回滚,则最大保存时间为60天,60天之前的都将被清理掉 这里的值与滚动策略模式有关 -->
<maxHistory>60</maxHistory>
<!-- 按照日期进行归档,并且对日志文件大小进行限制 -->
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<!-- 设置文件大于1KB进行压缩归档 -->
<maxFileSize>10MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
<encoder><!-- 格式化输出 -->
<pattern>[%token] %d %-5level %logger[%line] - %msg%n</pattern>
</encoder>
</appender>
<!-- 输出到文件 -->
<appender name="INFO_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>INFO</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
<file>${LOG_PATH}/info.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${LOG_PATH}/info.log.%d{yyyy-MM-dd}.%i.log</fileNamePattern>
<maxHistory>60</maxHistory>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>10MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
<encoder>
<pattern>[%token] %d %-5level %logger[%line] - %msg%n</pattern>
</encoder>
</appender>
<appender name="WARN_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>WARN</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
<file>${LOG_PATH}/warn.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${LOG_PATH}/warn.log.%d{yyyy-MM-dd}.%i.log</fileNamePattern>
<maxHistory>60</maxHistory>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>10MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
<encoder>
<pattern>[%token] %d %-5level %logger[%line] - %msg%n</pattern>
</encoder>
</appender>
<appender name="ERROR_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>ERROR</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
<file>${LOG_PATH}/error.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${LOG_PATH}/error.log.%d{yyyy-MM-dd}.%i.log</fileNamePattern>
<maxHistory>60</maxHistory>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>10MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
<encoder>
<pattern>[%token] %d %-5level %logger[%line] - %msg%n</pattern>
</encoder>
</appender>
<root level="info">
<appender-ref ref="CONSOLE" />
<appender-ref ref="INFO_FILE" />
<appender-ref ref="WARN_FILE" />
<appender-ref ref="ERROR_FILE" />
</root>
<springProfile name="dev,test">
<logger name="com.viewhigh.vhsc.hospital" level="debug" additivity="false">
<appender-ref ref="CONSOLE" />
<appender-ref ref="DEBUG_FILE" />
<appender-ref ref="INFO_FILE" />
<appender-ref ref="WARN_FILE" />
<appender-ref ref="ERROR_FILE" />
</logger>
</springProfile>
<springProfile name="demo,pro">
<logger name="com.viewhigh.vhsc.hospital" level="info" additivity="false">
<appender-ref ref="CONSOLE" />
<appender-ref ref="INFO_FILE" />
<appender-ref ref="WARN_FILE" />
<appender-ref ref="ERROR_FILE" />
</logger>
</springProfile>
</configuration>
import com.viewhigh.vhsc.platform.common.constant.Const;
import com.viewhigh.vhsc.platform.support.LocalThreadContext;
import ch.qos.logback.classic.pattern.ClassicConverter;
import ch.qos.logback.classic.spi.ILoggingEvent;
/**
* @Title: TokenConvert.java
* @Package com.viewhigh.vhsc.platform.log
* @Description: 当前请求线程调用链token转换器
* @author cctv
* @Company www.cctv.com
* @date 2013年8月11日 下午7:45:31
* @version V2.0
*/
public class LogThreadTokenConvert extends ClassicConverter {
@Override
public String convert(ILoggingEvent event) {
return LogThreadTokenConvert.getThreadToken();
}
public static String getThreadToken() {
String token = (String) LocalThreadContext.getContext().get(Const.TOKEN_KEY);
if (token == null) {
return genThreadToken();
}
return token;
}
public static void clearToken() {
LocalThreadContext.getContext().remove(Const.TOKEN_KEY);
}
private static String genThreadToken() {
long time = System.currentTimeMillis();
long threadId = Thread.currentThread().getId();
String token = time + "-" + threadId;
LocalThreadContext.getContext().put(Const.TOKEN_KEY, token);
return token;
}
public static void main(String[] args) {
System.out.println(genThreadToken());
}
}
import ch.qos.logback.classic.PatternLayout;
/**
* @author cctv
* @ClassName: CustomPatternLayout
* @Description: 日志装饰类,加入全局调用链唯一号
* @date 2014年01月19日 下午8:10:06
*/
public class CustomPatternLayout extends PatternLayout {
static {
defaultConverterMap.put("token", LogThreadTokenConvert.class.getName());
}
}
import java.util.Arrays;
import java.util.List;
/**
* @ClassName: Const
* @Description: 常量
* @author cctv
* @date 2017年8月4日 下午6:07:28
*/
public class Const {
// token(用于表单重复提交|日志线程唯一ID)
public static final String TOKEN_KEY = "token";
}
以下是spring boot application.yml对日志的配置
# 日志配置
logging:
path: logs/
config: classpath:cfg/logback-spring.xml