SpringBoot(⼋)----SpringBoot配置⽇志⽂件
今天介绍⼀下SpringBoot配置⽇志⽂件
SpringBoot在所有的内部⽇志中使⽤Commons Logging,但是默认配置也提供了对常⽤⽇志的⽀持,如Java Util Logging,Log4J,Log4J2和Logback。但是每种Logger都可以通过配置使⽤控制台或者⽂件输出⽇志内容。
⼀.SpringBoot默认⽇志Logback
SLF4J,是⼀个针对各类Java⽇志框架的统⼀Façade抽象。Java有很多的⽇志框架,如java.util.logging,log4j,logback,commons-logging,Spring框架使⽤的是Jakarta Commons Logging API(JCL)。⽽SLF4J定义了统⼀的⽇志抽象接⼝,⽽真正的⽇志实现则是在运⾏决定的。
Logback是log4j框架的作者开发的新⼀代⽇志框架,它能够适应诸多运⾏环境,⽀持SLF4J。
默认情况下,SpringBoot使⽤SLF4J+Logback记录⽇志。
⼆.⽇志输出的内容元素
⽇志输出的内容元素具体如下:
时间⽇期:精确到毫秒
⽇志级别:ERROR,WARN,INFO,DEBUG,TRACE
进程ID
分隔符:---标识实际⽇志的开始
线程名:⽅括号括起来的内容
Logger名:通常为源代码类名
⽇志内容
三.添加⽇志依赖
⽹上给出的⽇志依赖为:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</dependency>
Spring boot应⽤将⾃动使⽤logback作为应⽤⽇志框架,Spring Boot启动的时候,由org.springframework.boot.logging.Logging-Application-Listener根据情况初始化并使⽤。spring-boot-starter中包含spring-boot-starter-logging,该依赖内容就是Spring boot默认的⽇志框架logback。
我⾃⼰使⽤的依赖如下:
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.20</version>
</dependency>
<!-- 添加logback-classic依赖 -->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
</dependency>
<!-- 添加logback-core依赖 -->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
</dependency>
四.控制台输出
⽇志级别从低到⾼分别是TRACE<DEBUG<INFO<WARN<ERROR<FATAL,如果设置为WARN,则低于WARN的信息都不会被输出。
SpringBoot中默认配置ERROR、WARN和INFO级别的⽇志输出到控制台。
在application.properties中配置logging.level.包名=‘⽇志级别’,可以控制控制台输出⽇志级别,举个例⼦:
⾸先,建⼀个springboot项⽬,项⽬⽬录如下:
第⼆步,配置l⽂件,加⼊jar包
<project xmlns="/POM/4.0.0"
xmlns:xsi="/2001/XMLSchema-instance"
xsi:schemaLocation="/POM/4.0.0 /maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>springboot_002</groupId>
<artifactId>springboot_002</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>springboot_002 Maven Webapp</name>
<url></url>
<dependencies>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.20</version>
</dependency>
<!-- 添加logback-classic依赖 -->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
</dependency>
<!-- 添加logback-core依赖 -->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<!-- 继承⽗包 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.3.RELEASE</version>
</parent>
<build>
<finalName>springboot_002</finalName>
</build>
</project>
第三步,在src/main/java⽬录下建包springboot,并创建⼀个SpringBootApplicationFirst.java启动类。
SpringBootApplicationFirst.java
package springboot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.support.SpringBootServletInitializer;
@SpringBootApplication
public class SpringBootApplicationFirst extends SpringBootServletInitializer{
public static void main(String[]args){
SpringApplication.run(SpringBootApplicationFirst.class, args);
}
}
第四步,在src/main/resources⽬录下配置application.properties⽂件,在application.properties中配置⽇志等级,如下:
logging.level.zk=debug
这⾥的⽇志级别等级可以更改。
最后,我们在src/test/java下创建包springboot(测试类包名需要与启动类包名保持⼀致),并在包中创建⼀个测试类SpringBootApplicationTest.java。SpringBootApplicationTest.java
package springboot;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.st.context.SpringBootTest;
import st.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringBootApplicationTests {
//⽇志的级别
/*
* ⽇志的级别是由⾼到低的
* SpringBoot默认⽇志级别---info级别
* trace>debug>info>warn>error
*/
//记录器
Logger Logger(getClass());
@Test
public void contextLoads() {
logger.debug("这是debug⽇志...");
logger.info("这是info⽇志...");
logger.warn("这是warn⽇志...");
<("这是error⽇志...");
}
}
我们启动测试类,执⾏结果如下: 
可以看到⽇志的输出内容。
当然我们可以通过配置属性⽂件中的logging.file或者logging.path将⽇志⽂件输出到单独的⽂件中,需要注意的的是,这两个配置⽂件的属性值不可同时使⽤,否则只有logging.file⽣效。
⼀.如果我们配置
springboot切换log4j2
logging.file=SpringBoot.log
则在⼯程⽬录下会⽣成⼀个SpringBoot.log⽂件。
⽇志⽂件内容如下:
2020-04-08 16:38:31.006  INFO 20008 --- [main] SpringBootApplicationTests        : Starting SpringBootApplicationTests on zhangkun0400 with PID 20008 (started by zhangkun04 in D:\eclipseworkspace\springboot_002)
2020-04-08 16:38:31.006 DEBUG 20008 --- [main] SpringBootApplicationTests        : Running with Spring Boot v1.4.3.RELEASE, Spring v4.3.5.RELEASE
2020-04-08 16:38:31.007  INFO 20008 --- [main] SpringBootApplicationTests        : No active profile set, falling back to default profiles: default
2020-04-08 16:38:31.036  INFO 20008 --- [main] o.s.GenericWebApplicationContext  : Refreshing org.t.support.GenericWebApplicationContext@5d0a1059: startup date [Wed Apr 08 16:38:31 CST 2020]; root of 2020-04-08 16:38:32.802  INFO 20008 --- [main] s.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.t.support.GenericWebApplicationContext@5d0a1059: startup date [Wed Apr 08 16:38 2020-04-08 16:38:32.842  INFO 20008 --- [main] s.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.htt
p.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boo 2020-04-08 16:38:32.844  INFO 20008 --- [main] s.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.Ba 2020-04-08 16:38:32.870  INFO 20008 --- [main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.source.ResourceHttpRequestHandler]
2020-04-08 16:38:32.870  INFO 20008 --- [main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**] onto handler of type [class org.springframework.source.ResourceHttpRequestHandler]
2020-04-08 16:38:32.901  INFO 20008 --- [main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.source.ResourceHttpRequestHandler]
2020-04-08 16:38:33.050  INFO 20008 --- [main] SpringBootApplicationTests        : Started SpringBootApplicationTests in 2.379 seconds (JVM running for 3.06)
2020-04-08 16:38:33.073 DEBUG 20008 --- [main] SpringBootApplicationTests        : 这是debug
⽇志...
2020-04-08 16:38:33.073  INFO 20008 --- [main] SpringBootApplicationTests        : 这是info⽇志...
2020-04-08 16:38:33.074  WARN 20008 --- [main] SpringBootApplicationTests        : 这是warn⽇志...
2020-04-08 16:38:33.074 ERROR 20008 --- [main] SpringBootApplicationTests        : 这是error⽇志...
2020-04-08 16:38:33.080  INFO 20008 --- [Thread-1] o.s.GenericWebApplicationContext  : Closing org.t.support.GenericWebApplicationContext@5d0a1059: startup date [Wed Apr 08 16:38:31 CST 2020]; root of 2020-04-08 16:46:06.593  INFO 14984 --- [main] SpringBootApplicationTests        : Starting SpringBootApplicationTests on zhangkun0400 with PID 14984 (started by zhangkun04 in D:\eclipseworkspace\springboot_002)
2020-04-08 16:46:06.594 DEBUG 14984 --- [main] SpringBootApplicationTests        : Running with Spring Boot v1.4.3.RELEASE, Spring v4.3.5.RELEASE
2020-04-08 16:46:06.594  INFO 14984 --- [main] SpringBootApplicationTests        : No active profile set, falling back to default profiles: default
2020-04-08 16:46:06.623  INFO 14984 --- [main] o.s.GenericWebApplicationContext  : Refreshing org.t.support.GenericWebApplicationContext@5d0a1059: startup date [Wed Apr 08 16:46:06 CST 2020]; root of 2020-04-08 16:46:07.968  INFO 14984 --- [main] s.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.t.support.GenericWebApplicationContext@5d0a1059: startup date [Wed Apr 08 16:46 2020-04-08 16:46:08.052  INFO 14984 --- [main] s.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boo 2020-04-08 16:46:08.053  INFO 14984 --- [main] s.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.Ba 2020-04-08 16:46:08.101  INFO 14984 --- [main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.source.ResourceHttpRequestHandler]
2020-04-08 16:46:08.102  INFO 14984 --- [main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**] onto handler of type [class org.springframework.source.ResourceHttpRequestHandler]
2020-04-08 16:46:08.157  INFO 14984 --- [main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.source.ResourceHttpRequestHandler]
2020-04-08 16:46:08.410  INFO 14984 --- [main] SpringBootApplicationTests        : Started SpringBootApplicationTests in 2.123 seconds (JVM running for 2.649)
2020-04-08 16:46:08.449 DEBUG 14984 --- [main] SpringBootApplicationTests        : 这是debug⽇志...
2020-04-08 16:46:08.450  INFO 14984 --- [main] SpringBootApplicationTests        : 这是info⽇志...
2020-04-08 16:46:08.450  WARN 14984 --- [main] SpringBootApplicationTests        : 这是warn⽇志...
2020-04-08 16:46:08.451 ERROR 14984 --- [main] SpringBootApplicationTests        : 这是error⽇志...
2020-04-08 16:46:08.460  INFO 14984 --- [Thread-1] o.s.GenericWebApplicationContext  : Closing org.t.support.GenericWebApplicationContext@5d0a1059: startup date [Wed Apr 08 16:46:06 CST 2020]; root of 2020-04-08 21:07:59.766  INFO 15932 --- [main] SpringBootApplicationTests        : Starting SpringBootApplicationTests on zhangkun0400 with PID 15932 (started by zhangkun04 in D:\eclipseworkspace\springboot_002)
2020-04-08 21:07:59.768 DEBUG 15932 --- [main] SpringBootApplicationTests        : Running with Spring Boot v1.4.3.RELEASE, Spring v4.3.5.RELEASE
2020-04-08 21:07:59.769  INFO 15932 --- [main] SpringBootApplicationTests        : No active profile set, falling back to default profiles: default
2020-04-08 21:07:59.814  INFO 15932 --- [main] o.s.GenericWebApplicationContext  : Refreshing org.t.support.GenericWebApplicationContext@5d0a1059: startup date [Wed Apr 08 21:07:59 CST 2020]; root of 2020-04-08 21:08:01.360  INFO 15932 --- [main] s.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.w
2020-04-08 21:08:01.448  INFO 15932 --- [main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**] onto handler of type [class org.springframework.source.ResourceHttpRequestHandler]
2020-04-08 21:08:01.487  INFO 15932 --- [main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.source.ResourceHttpRequestHandler]
2020-04-08 21:08:01.639  INFO 15932 --- [main] SpringBootApplicationTests        : Started SpringBootApplicationTests in 2.275 seconds (JVM running for 3.022)
2020-04-08 21:08:01.661 DEBUG 15932 --- [main] SpringBootApplicationTests        : 这是debug⽇志...
2020-04-08 21:08:01.661  INFO 15932 --- [main] SpringBootApplicationTests        : 这是info⽇志...
2020-04-08 21:08:01.661  WARN 15932 --- [main] SpringBootApplicationTests        : 这是warn⽇志...
2020-04-08 21:08:01.661 ERROR 15932 --- [main] SpringBootApplicationTests        : 这是error⽇志...
2020-04-08 21:08:01.665  INFO 15932 --- [Thread-1] o.s.GenericWebApplicationContext  : Closing org.t.support.GenericWebApplicationContext@5d0a1059: startup date [Wed Apr 08 21:07:59 CST 2020]; root of ⼆.如果我们配置的是loggin.path,如下:
logging.path=/spring/log
则会在我们项⽬的磁盘下创建⼀个log⽂件,
spring.log⽇志⽂件内容如下:
2020-04-08 16:51:48.278  INFO 16368 --- [main] SpringBootApplicationTests        : Starting SpringBootApplicationTests on zhangkun0400 with PID 16368 (started by zhangkun04 in D:\eclipseworkspace\springboot_002)
2020-04-08 16:51:48.279 DEBUG 16368 --- [main] SpringBootApplicationTests        : Running with Spring Boot v1.4.3.RELEASE, Spring v4.3.5.RELEASE
2020-04-08 16:51:48.280  INFO 16368 --- [main] SpringBootApplicationTests        : No active profile set, falling back to default profiles: default
2020-04-08 16:51:48.323  INFO 16368 --- [main] o.s.GenericWebApplicationContext  : Refreshing org.t.support.GenericWebApplicationContext@5d0a1059: star
tup date [Wed Apr 08 16:51:48 CST 2020]; root of 2020-04-08 16:51:51.273  INFO 16368 --- [main] s.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.t.support.GenericWebApplicationContext@5d0a1059: startup date [Wed Apr 08 16:51 2020-04-08 16:51:51.402  INFO 16368 --- [main] s.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boo 2020-04-08 16:51:51.404  INFO 16368 --- [main] s.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.Ba 2020-04-08 16:51:51.469  INFO 16368 --- [main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.source.ResourceHttpRequestHandler]
2020-04-08 16:51:51.470  INFO 16368 --- [main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**] onto handler of type [class org.springframework.source.ResourceHttpRequestHandler]
2020-04-08 16:51:51.560  INFO 16368 --- [main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped
URL path [/**/favicon.ico] onto handler of type [class org.springframework.source.ResourceHttpRequestHandler]
2020-04-08 16:51:51.936  INFO 16368 --- [main] SpringBootApplicationTests        : Started SpringBootApplicationTests in 4.249 seconds (JVM running for 5.196)
2020-04-08 16:51:51.996 DEBUG 16368 --- [main] SpringBootApplicationTests        : 这是debug⽇志...
2020-04-08 16:51:51.997  INFO 16368 --- [main] SpringBootApplicationTests        : 这是info⽇志...
2020-04-08 16:51:51.998  WARN 16368 --- [main] SpringBootApplicationTests        : 这是warn⽇志...
2020-04-08 16:51:51.999 ERROR 16368 --- [main] SpringBootApplicationTests        : 这是error⽇志...
2020-04-08 16:51:52.038  INFO 16368 --- [Thread-1] o.s.GenericWebApplicationContext  : Closing org.t.support.GenericWebApplicationContext@5d0a1059: startup
date [Wed Apr 08 16:51:48 CST 2020]; root of 我们可以通过sole属性或者logging.pattern.file属性来设置我们的⽇志⽂件输出格式。
sole=%d{yyyy-MM-dd HH:mm:ss:SSS}----> [%thread]----> %-5level----> %logger{50}----> -%msg%n 
logging.pattern.file=%d{yyyy-MM-dd HH:mm:ss:SSS}----> [%thread]----> %-5level----> %logger{50}----> -%msg%n
 输出内容如下:
五.⾃定义配置⽂件
5.1配置单个l环境
我们可以在项⽬中⾃定义单个l⽂件,配置对应的⽇志⽂件信息。
⼯程⽬录如下:
添加l
<?xml version="1.0" encoding="UTF-8"?>
<configuration
xmlns:xsi="/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="www.padual/java/logback.xsd"
debug="false" scan="true" scanPeriod="30 second">
<property name="PROJECT" value="iorder" />
<property name="ROOT" value="logs/${PROJECT}/" />
<property name="FILESIZE" value="50MB" />
<property name="MAXHISTORY" value="100" />
<timestamp key="DATETIME" datePattern="yyyy-MM-dd HH:mm:ss" />
<!-- 控制台打印 -->
<appender name="STDOUT" class="ch.ConsoleAppender">
<encoder charset="utf-8">
<pattern>[%-5level]---> %d{${DATETIME}}---> [%thread]---> %logger{36}---> - %m%n
</pattern>
</encoder>
</appender>
<!-- ERROR 输⼊到⽂件,按⽇期和⽂件⼤⼩ -->
<appender name="ERROR" class="ch.olling.RollingFileAppender">
<encoder charset="utf-8">
<pattern>[%-5level]---> %d{${DATETIME}}---> [%thread]---> %logger{36}---> - %m%n
</pattern>
</encoder>
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>ERROR</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
<rollingPolicy class="ch.olling.TimeBasedRollingPolicy">
<fileNamePattern>${ROOT}%d/error.%i.log</fileNamePattern>
<maxHistory>${MAXHISTORY}</maxHistory>
<timeBasedFileNamingAndTriggeringPolicy
class="ch.olling.SizeAndTimeBasedFNATP">
<maxFileSize>${FILESIZE}</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
</appender>
<!-- WARN 输⼊到⽂件,按⽇期和⽂件⼤⼩ -->
<appender name="WARN" class="ch.olling.RollingFileAppender">
<encoder charset="utf-8">
<pattern>[%-5level]---> %d{${DATETIME}}---> [%thread]---> %logger{36}---> - %m%n
</pattern>
</encoder>
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>WARN</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
<rollingPolicy class="ch.olling.TimeBasedRollingPolicy">
<fileNamePattern>${ROOT}%d/warn.%i.log</fileNamePattern>
<maxHistory>${MAXHISTORY}</maxHistory>
<timeBasedFileNamingAndTriggeringPolicy class="ch.olling.SizeAndTimeBasedFNATP">
<maxFileSize>${FILESIZE}</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
</appender>
<!-- INFO 输⼊到⽂件,按⽇期和⽂件⼤⼩ -->
<appender name="INFO" class="ch.olling.RollingFileAppender">
<encoder charset="utf-8">
<pattern>[%-5level] %d{${DATETIME}} [%thread] %logger{36} - %m%n
</pattern>
</encoder>
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>INFO</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
<rollingPolicy class="ch.olling.TimeBasedRollingPolicy">
<fileNamePattern>${ROOT}%d/info.%i.log</fileNamePattern>
<maxHistory>${MAXHISTORY}</maxHistory>
<timeBasedFileNamingAndTriggeringPolicy class="ch.olling.SizeAndTimeBasedFNATP">
<maxFileSize>${FILESIZE}</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
</appender>
<!-- DEBUG 输⼊到⽂件,按⽇期和⽂件⼤⼩ -->
<appender name="DEBUG" class="ch.olling.RollingFileAppender">
<encoder charset="utf-8">
<pattern>[%-5level] %d{${DATETIME}} [%thread] %logger{36} - %m%n
</pattern>
</encoder>
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>DEBUG</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
<rollingPolicy class="ch.olling.TimeBasedRollingPolicy">
<fileNamePattern>${ROOT}%d/debug.%i.log</fileNamePattern>
<maxHistory>${MAXHISTORY}</maxHistory>
<timeBasedFileNamingAndTriggeringPolicy
class="ch.olling.SizeAndTimeBasedFNATP">
<maxFileSize>${FILESIZE}</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
</appender>
<!-- TRACE 输⼊到⽂件,按⽇期和⽂件⼤⼩ -->
<appender name="TRACE" class="ch.olling.RollingFileAppender">
<encoder charset="utf-8">
<pattern>[%-5level] %d{${DATETIME}} [%thread] %logger{36} - %m%n
</pattern>
</encoder>
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>TRACE</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
<rollingPolicy
class="ch.olling.TimeBasedRollingPolicy">
<fileNamePattern>${ROOT}%d/trace.%i.log</fileNamePattern>
<maxHistory>${MAXHISTORY}</maxHistory>
<timeBasedFileNamingAndTriggeringPolicy
class="ch.olling.SizeAndTimeBasedFNATP">
<maxFileSize>${FILESIZE}</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
</appender>
<!-- SQL相关⽇志输出-->
<logger name="org.apache.ibatis" level="INFO" additivity="false" />
<logger name="batis.spring" level="INFO" additivity="false" />
<logger name="com.batis.paginator" level="INFO" additivity="false" />
<!-- Logger 根⽬录 -->
<root level="DEBUG">
<appender-ref ref="STDOUT" />
<appender-ref ref="DEBUG" />
<appender-ref ref="ERROR" />
<appender-ref ref="WARN" />
<appender-ref ref="INFO" />
<appender-ref ref="TRACE" />
</root>
</configuration>
 可以在l中配置对应的⽇志信息。启动程序输出内容如下:
5.2配置多个logback环境
我们也可以在l⽂件中配置多个logback环境,具体l配置如下:
⾸先将l更改命名为l,需要更改⽂件名,否则⽆法识别<spring-Profile>标签。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。