springboot 集成了logback日志系统,默认读取logbak配置文件,配置文件的名称默认:logback-spring.xml,如果想自定义配置文件的名称,需要在application.yml配置文件中作如下配置来指定logback的配置文件
logging:
config: classpath:logback.xml
appender配置详解
appender 的种类
logback-spring.xml
<configuration>
<property name="LOG_CONTEXT_NAME" value="chongba"/>
<property name="LOG_HOME" value="logs/${LOG_CONTEXT_NAME}" />
<contextName>${LOG_CONTEXT_NAME}contextName>
<appender name="console" class="ch.qos.logback.core.ConsoleAppender">
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{56}.%method:%L - %msg%npattern>
<charset>utf-8charset>
encoder>
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>INFOlevel>
filter>
appender>
<appender name="file.info" class="ch.qos.logback.core.rolling.RollingFileAppender">
<Prudent>truePrudent>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<FileNamePattern>${LOG_HOME}/%d{yyyy-MM-dd}/info/info.%d{yyyy-MM-dd-HH}.%i.logFileNamePattern>
<MaxHistory>30MaxHistory>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>10MBmaxFileSize>
timeBasedFileNamingAndTriggeringPolicy>
rollingPolicy>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<pattern> %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{56}.%method:%L - %msg%npattern>
<charset>utf-8charset>
encoder>
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>INFOlevel>
<onMatch>ACCEPTonMatch>
<onMismatch>DENYonMismatch>
filter>
appender>
<appender name="file.error" class="ch.qos.logback.core.rolling.RollingFileAppender">
<Prudent>truePrudent>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<FileNamePattern>${LOG_HOME}/%d{yyyy-MM-dd}/error/error.%d{yyyy-MM-dd}.%i.logFileNamePattern>
<MaxHistory>30MaxHistory>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>10MBmaxFileSize>
timeBasedFileNamingAndTriggeringPolicy>
rollingPolicy>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<pattern> %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{56}.%method:%L - %msg%npattern>
<charset>utf-8charset>
encoder>
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>ERRORlevel>
<onMatch>ACCEPTonMatch>
<onMismatch>DENYonMismatch>
filter>
appender>
<appender name="file.warn" class="ch.qos.logback.core.rolling.RollingFileAppender">
<Prudent>truePrudent>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<FileNamePattern>${LOG_HOME}/%d{yyyy-MM-dd}/warn/warn.%d{yyyy-MM-dd}.%i.logFileNamePattern>
<MaxHistory>30MaxHistory>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>10MBmaxFileSize>
timeBasedFileNamingAndTriggeringPolicy>
rollingPolicy>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<pattern> %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{56}.%method:%L - %msg%npattern>
<charset>utf-8charset>
encoder>
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>WARNlevel>
<onMatch>ACCEPTonMatch>
<onMismatch>DENYonMismatch>
filter>
appender>
<appender name="file.thread" class="ch.qos.logback.core.rolling.RollingFileAppender">
<Prudent>truePrudent>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<FileNamePattern>${LOG_HOME}/%d{yyyy-MM-dd}/thread/thread.%d{yyyy-MM-dd}.%i.logFileNamePattern>
<MaxHistory>30MaxHistory>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>100MBmaxFileSize>
timeBasedFileNamingAndTriggeringPolicy>
rollingPolicy>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<pattern> %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{56}.%method:%L - %msg%npattern>
<charset>utf-8charset>
encoder>
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>INFOlevel>
filter>
appender>
<root level="debug">
<appender-ref ref="console" />
<appender-ref ref="file.error" />
<appender-ref ref="file.info" />
<appender-ref ref="file.warn" />
root>
<logger name="thread" additivity="false">
<appender-ref ref="file.thread"/>
<appender-ref ref="console" />
logger>
<logger name="com.chongba.schedule.service.VisiableThreadPool" additivity="false">
<appender-ref ref="file.thread"/>
logger>
configuration>
线程池日志监控
@Slf4j
public class VisiableThreadPool extends ThreadPoolTaskExecutor{
/**
* 收集线程池运行参数信息的方法
* @param info
*/
private void logs(String info){
/**
* - getThreadNamePrefix:线程池内线程名称前缀,方便定位
- getTaskCount:线程池已经执行的和未执行的任务总数;
- getCompletedTaskCount:线程池已完成的任务数量,该值小于等于taskCount;
- getActiveCount:当前线程池中正在执行任务的线程数量。
- queueSize:缓冲队列大小。
*/
String prefix = this.getThreadNamePrefix();
long taskCount = this.getThreadPoolExecutor().getTaskCount();
long completedTaskCount = this.getThreadPoolExecutor().getCompletedTaskCount();
int activeCount = this.getThreadPoolExecutor().getActiveCount();
int queueSize = this.getThreadPoolExecutor().getQueue().size();
log.info("prefix={},info={},taskCount={},completedCount={},activeCount={},queueSize={}",prefix,
info,taskCount,completedTaskCount,activeCount,queueSize);
}
@Override
public void execute(Runnable task) {
super.execute(task);
logs("do execute");
}
@Override
public <T> Future<T> submit(Callable<T> task) {
Future<T> future = super.submit(task);
logs("do submit");
return future;
}
}
常用logback配置
<configuration>
<property name="LOG_HOME" value="D:/logs"/>
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%npattern>
<charset>utf8charset>
encoder>
appender>
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${LOG_HOME}/leadnews.%d{yyyy-MM-dd}.logfileNamePattern>
rollingPolicy>
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%npattern>
encoder>
appender>
<appender name="ASYNC" class="ch.qos.logback.classic.AsyncAppender">
<discardingThreshold>0discardingThreshold>
<queueSize>512queueSize>
<appender-ref ref="FILE"/>
appender>
<logger name="org.apache.ibatis.cache.decorators.LoggingCache" level="DEBUG" additivity="false">
<appender-ref ref="CONSOLE"/>
logger>
<logger name="com.example.logback" level="warn" />
<root level="info">
<appender-ref ref="FILE"/>
<appender-ref ref="CONSOLE"/>
root>
configuration>