1. SpringBoot study notes thirteen: logging framework springBoot learning
Article Directory
1.1. Common logging framework
- slf4j,log4j,logback,common-logging等
- logback is modified based log4j basis, can not be used alone, with the recommended SLF4j logback used, the current is divided into three modules: logback-core, logback-classic and logback-access; wherein logback-core is the basis for the other two modules modules .
- logback core object
logger: Logger
Appender: Specify the log output destination, the destination can be a console, file
Layout: layout, formatting the log output log.
- Level log: Debug <INFO <WARN <ERROR
- log4j.properties log transfer logback.xml, support properties turn into logback.xml
log4j.properties log transfer logback.xml
- The default log content logback
1, log creation date
2, log level
3, processId: process id
4, delimiter
5, thread name
6, the output of the log class
7, log information
1.2. SpringBoot2.x comes logBack log
- Creating logback-spring.xml configuration file in the resource folder inside
- The log4j.properties turn into logback.xml
<?xml version="1.0" encoding="UTF-8" ?>
<configuration>
<appender name="consoleApp" class="ch.qos.logback.core.ConsoleAppender">
<layout class="ch.qos.logback.classic.PatternLayout"><!-- 配置布局 -->
<pattern><!-- 输出日志的匹配,包含正则匹配 -->
%date{yyyy-MM-dd HH:mm:ss.SSS} %-5level[%thread]%logger{56}.%method:%L -%msg%n
</pattern>
</layout>
</appender>
<appender name="fileInfoApp" class="ch.qos.logback.core.rolling.RollingFileAppender">
<filter class="ch.qos.logback.classic.filter.LevelFilter"><!-- 过滤器,只保存info级别的日志 -->
<level>ERROR</level><!-- Error级别 -->
<onMatch>DENY</onMatch><!-- deny,不进行记录 -->
<onMismatch>ACCEPT</onMismatch>
</filter>
<encoder><!-- 与Layout一样,日志输出布局配置 -->
<pattern>
%date{yyyy-MM-dd HH:mm:ss.SSS} %-5level[%thread]%logger{56}.%method:%L -%msg%n
</pattern>
</encoder>
<!-- 滚动策略,根据时间日期滚动的 -->
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- 路径 -->
<fileNamePattern>app_log/log/app.info.%d.log</fileNamePattern>
</rollingPolicy>
</appender>
<appender name="fileErrorApp" class="ch.qos.logback.core.rolling.RollingFileAppender">
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>ERROR</level><!-- 不写其他表示只接受error级别配置 -->
</filter>
<encoder>
<pattern>
%date{yyyy-MM-dd HH:mm:ss.SSS} %-5level[%thread]%logger{56}.%method:%L -%msg%n
</pattern>
</encoder>
<!-- 设置滚动策略 -->
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- 路径 -->
<fileNamePattern>app_log/log/app.err.%d.log</fileNamePattern>
<!-- 控制保留的归档文件的最大数量,超出数量就删除旧文件,假设设置每个月滚动,
且<maxHistory> 是1,则只保存最近1个月的文件,删除之前的旧文件 -->
<MaxHistory>1</MaxHistory>
</rollingPolicy>
</appender>
<root level="INFO"> <!-- 控制日志输出级别 -->
<appender-ref ref="consoleApp"/> <!-- 这个输出到控制台consoleApp的appender级别中去 -->
<appender-ref ref="fileInfoApp"/><!-- 输出到文件,并且以滚动的格式,根据天、或者小时进行滚动 -->
<appender-ref ref="fileErrorApp"/>
</root>
</configuration>
- Interface test log
private Logger logger=LoggerFactory.getLogger(this.getClass());
@GetMapping("log")
public Object testlog() {
logger.debug("this is debug");
logger.info("this is info");
logger.warn("this is warn");
logger.error("this is error");
return JsonData.buildSuccess();
}
- Note that the imported package
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;