“Spring Boot 日志配置”的版本间的差异
跳到导航
跳到搜索
Jihongchang(讨论 | 贡献) |
Jihongchang(讨论 | 贡献) |
||
第229行: | 第229行: | ||
# 指定控制台输出消息格式 | # 指定控制台输出消息格式 | ||
logging.pattern.console=[%-5level] %d{yyyy-MM-dd HH:mm:ss} %c [%thread] %msg%n | logging.pattern.console=[%-5level] %d{yyyy-MM-dd HH:mm:ss} %c [%thread] %msg%n | ||
+ | </syntaxhighlight>其他不变<syntaxhighlight lang="console"> | ||
+ | 18:32:25.567 [main] DEBUG org.springframework.test.context.BootstrapUtils - Instantiating CacheAwareContextLoaderDelegate from class [org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate] | ||
+ | …… | ||
+ | |||
+ | . ____ _ __ _ _ | ||
+ | /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ | ||
+ | ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ | ||
+ | \\/ ___)| |_)| | | | | || (_| | ) ) ) ) | ||
+ | ' |____| .__|_| |_|_| |_\__, | / / / / | ||
+ | =========|_|==============|___/=/_/_/_/ | ||
+ | :: Spring Boot :: (v2.2.2.RELEASE) | ||
+ | |||
+ | [INFO ] 2023-02-28 18:32:25 io.github.jihch.SpringBootLogApplicationTests [main] Starting SpringBootLogApplicationTests on SK-20210414CLPG with PID 35396 (started by Administrator in E:\record\2023\2\27\spring-boot-log) | ||
+ | [DEBUG] 2023-02-28 18:32:25 io.github.jihch.SpringBootLogApplicationTests [main] Running with Spring Boot v2.2.2.RELEASE, Spring v5.2.2.RELEASE | ||
+ | [INFO ] 2023-02-28 18:32:25 io.github.jihch.SpringBootLogApplicationTests [main] No active profile set, falling back to default profiles: default | ||
+ | [INFO ] 2023-02-28 18:32:26 org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor [main] Initializing ExecutorService 'applicationTaskExecutor' | ||
+ | [INFO ] 2023-02-28 18:32:26 io.github.jihch.SpringBootLogApplicationTests [main] Started SpringBootLogApplicationTests in 1.091 seconds (JVM running for 1.862) | ||
+ | [ERROR] 2023-02-28 18:32:27 io.github.jihch.SpringBootLogApplicationTests [main] error | ||
+ | [WARN ] 2023-02-28 18:32:27 io.github.jihch.SpringBootLogApplicationTests [main] warn | ||
+ | [INFO ] 2023-02-28 18:32:27 io.github.jihch.SpringBootLogApplicationTests [main] info | ||
+ | [DEBUG] 2023-02-28 18:32:27 io.github.jihch.SpringBootLogApplicationTests [main] debug | ||
+ | [TRACE] 2023-02-28 18:32:27 io.github.jihch.SpringBootLogApplicationTests [main] trace | ||
+ | [INFO ] 2023-02-28 18:32:27 io.github.jihch.SpringBootLogApplicationTests [main] log4j 2 info | ||
+ | [INFO ] 2023-02-28 18:32:27 org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor [SpringContextShutdownHook] Shutting down ExecutorService 'applicationTaskExecutor' | ||
+ | |||
+ | Process finished with exit code 0 | ||
+ | |||
</syntaxhighlight> | </syntaxhighlight> |
2023年2月28日 (二) 10:33的版本
https://www.bilibili.com/video/BV1iJ411H74S?p=37
示例 无配置文件 默认使用 SLF4J 日志门面 + Logback 日志实现
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>spring-boot-starter-parent</artifactId>
<groupId>org.springframework.boot</groupId>
<version>2.2.2.RELEASE</version>
</parent>
<groupId>io.github.jihch</groupId>
<artifactId>spring-boot-log</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
</project>
SpringBootLogApplication.java
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringBootLogApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootLogApplication.class, args);
}
}
SpringBootLogApplicationTests.java
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
public class SpringBootLogApplicationTests {
// 声明日志记录器对象
public static final Logger LOGGER = LoggerFactory.getLogger(SpringBootLogApplicationTests.class);
@Test
public void contextLoads() {
// 打印日志信息
LOGGER.error("error");
LOGGER.warn("warn");
LOGGER.info("info"); // 默认日志级别
LOGGER.debug("debug");
LOGGER.trace("trace");
}
}
18:07:03.230 [main] DEBUG org.springframework.test.context.BootstrapUtils - Instantiating CacheAwareContextLoaderDelegate from class [org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate]
……
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.2.2.RELEASE)
2023-02-28 18:07:03.751 INFO 47472 --- [ main] i.g.jihch.SpringBootLogApplicationTests : Starting SpringBootLogApplicationTests on SK-20210414CLPG with PID 47472 (started by Administrator in E:\record\2023\2\27\spring-boot-log)
2023-02-28 18:07:03.752 INFO 47472 --- [ main] i.g.jihch.SpringBootLogApplicationTests : No active profile set, falling back to default profiles: default
2023-02-28 18:07:04.614 INFO 47472 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2023-02-28 18:07:04.780 INFO 47472 --- [ main] i.g.jihch.SpringBootLogApplicationTests : Started SpringBootLogApplicationTests in 1.271 seconds (JVM running for 2.006)
2023-02-28 18:07:04.900 ERROR 47472 --- [ main] i.g.jihch.SpringBootLogApplicationTests : error
2023-02-28 18:07:04.900 WARN 47472 --- [ main] i.g.jihch.SpringBootLogApplicationTests : warn
2023-02-28 18:07:04.901 INFO 47472 --- [ main] i.g.jihch.SpringBootLogApplicationTests : info
2023-02-28 18:07:04.911 INFO 47472 --- [extShutdownHook] o.s.s.concurrent.ThreadPoolTaskExecutor : Shutting down ExecutorService 'applicationTaskExecutor'
示例 无配置文件 使用 Log4j 2记录日志也还是会经由桥接器使用 SLF4J 日志门面 + Logback 日志实现
SpringBootLogApplicationTests.java
import org.apache.logging.log4j.LogManager;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
public class SpringBootLogApplicationTests {
// 声明日志记录器对象
public static final Logger LOGGER = LoggerFactory.getLogger(SpringBootLogApplicationTests.class);
@Test
public void contextLoads() {
// 打印日志信息
LOGGER.error("error");
LOGGER.warn("warn");
LOGGER.info("info"); // 默认日志级别
LOGGER.debug("debug");
LOGGER.trace("trace");
// 使用 Log4j 2 进行日志记录也还是会通过桥接器切换为 SLF4J 日志门面和 Logback 日志实现
org.apache.logging.log4j.Logger logger = LogManager.getLogger(SpringBootLogApplicationTests.class);
logger.info("log4j 2 info");
}
}
18:19:36.386 [main] DEBUG org.springframework.test.context.BootstrapUtils - Instantiating CacheAwareContextLoaderDelegate from class [org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate]
……
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.2.2.RELEASE)
2023-02-28 18:19:36.807 INFO 14180 --- [ main] i.g.jihch.SpringBootLogApplicationTests : Starting SpringBootLogApplicationTests on SK-20210414CLPG with PID 14180 (started by Administrator in E:\record\2023\2\27\spring-boot-log)
2023-02-28 18:19:36.808 INFO 14180 --- [ main] i.g.jihch.SpringBootLogApplicationTests : No active profile set, falling back to default profiles: default
2023-02-28 18:19:37.601 INFO 14180 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2023-02-28 18:19:37.768 INFO 14180 --- [ main] i.g.jihch.SpringBootLogApplicationTests : Started SpringBootLogApplicationTests in 1.125 seconds (JVM running for 1.82)
2023-02-28 18:19:37.889 ERROR 14180 --- [ main] i.g.jihch.SpringBootLogApplicationTests : error
2023-02-28 18:19:37.889 WARN 14180 --- [ main] i.g.jihch.SpringBootLogApplicationTests : warn
2023-02-28 18:19:37.889 INFO 14180 --- [ main] i.g.jihch.SpringBootLogApplicationTests : info
2023-02-28 18:19:37.890 INFO 14180 --- [ main] i.g.jihch.SpringBootLogApplicationTests : log4j 2 info
2023-02-28 18:19:37.904 INFO 14180 --- [extShutdownHook] o.s.s.concurrent.ThreadPoolTaskExecutor : Shutting down ExecutorService 'applicationTaskExecutor'
Process finished with exit code 0
观察控制台的输出可以发现使用 Log4j 2 的 API 输出和 SLF4J API 输出 pattern 是一样的,因为最终还是经由桥接器调用 SLF4J 作为日志门面 + Logback 作为日志实现进行日志记录
示例 指定自定义 logger 对象日志级别
application.properties
# 指定自定义 logger 对象日志级别
logging.level.io.github.jihch=trace
SpringBootLogApplication.java
不变
SpringBootLogApplicationTests.java
不变
18:24:42.138 [main] DEBUG org.springframework.test.context.BootstrapUtils - Instantiating CacheAwareContextLoaderDelegate from class [org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate]
……
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.2.2.RELEASE)
2023-02-28 18:24:42.610 INFO 18868 --- [ main] i.g.jihch.SpringBootLogApplicationTests : Starting SpringBootLogApplicationTests on SK-20210414CLPG with PID 18868 (started by Administrator in E:\record\2023\2\27\spring-boot-log)
2023-02-28 18:24:42.611 DEBUG 18868 --- [ main] i.g.jihch.SpringBootLogApplicationTests : Running with Spring Boot v2.2.2.RELEASE, Spring v5.2.2.RELEASE
2023-02-28 18:24:42.611 INFO 18868 --- [ main] i.g.jihch.SpringBootLogApplicationTests : No active profile set, falling back to default profiles: default
2023-02-28 18:24:43.358 INFO 18868 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2023-02-28 18:24:43.524 INFO 18868 --- [ main] i.g.jihch.SpringBootLogApplicationTests : Started SpringBootLogApplicationTests in 1.101 seconds (JVM running for 1.879)
2023-02-28 18:24:43.645 ERROR 18868 --- [ main] i.g.jihch.SpringBootLogApplicationTests : error
2023-02-28 18:24:43.646 WARN 18868 --- [ main] i.g.jihch.SpringBootLogApplicationTests : warn
2023-02-28 18:24:43.646 INFO 18868 --- [ main] i.g.jihch.SpringBootLogApplicationTests : info
2023-02-28 18:24:43.646 DEBUG 18868 --- [ main] i.g.jihch.SpringBootLogApplicationTests : debug
2023-02-28 18:24:43.646 TRACE 18868 --- [ main] i.g.jihch.SpringBootLogApplicationTests : trace
2023-02-28 18:24:43.647 INFO 18868 --- [ main] i.g.jihch.SpringBootLogApplicationTests : log4j 2 info
2023-02-28 18:24:43.658 INFO 18868 --- [extShutdownHook] o.s.s.concurrent.ThreadPoolTaskExecutor : Shutting down ExecutorService 'applicationTaskExecutor'
Process finished with exit code 0
观察控制台输出,可以看到对比前两次,这次还输出了 debug 和 trace
示例 指定控制台输出日志消息格式
application.properties
# 指定自定义 logger 对象日志级别
logging.level.io.github.jihch=trace
# 指定控制台输出消息格式
logging.pattern.console=[%-5level] %d{yyyy-MM-dd HH:mm:ss} %c [%thread] %msg%n
其他不变
18:32:25.567 [main] DEBUG org.springframework.test.context.BootstrapUtils - Instantiating CacheAwareContextLoaderDelegate from class [org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate]
……
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.2.2.RELEASE)
[INFO ] 2023-02-28 18:32:25 io.github.jihch.SpringBootLogApplicationTests [main] Starting SpringBootLogApplicationTests on SK-20210414CLPG with PID 35396 (started by Administrator in E:\record\2023\2\27\spring-boot-log)
[DEBUG] 2023-02-28 18:32:25 io.github.jihch.SpringBootLogApplicationTests [main] Running with Spring Boot v2.2.2.RELEASE, Spring v5.2.2.RELEASE
[INFO ] 2023-02-28 18:32:25 io.github.jihch.SpringBootLogApplicationTests [main] No active profile set, falling back to default profiles: default
[INFO ] 2023-02-28 18:32:26 org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor [main] Initializing ExecutorService 'applicationTaskExecutor'
[INFO ] 2023-02-28 18:32:26 io.github.jihch.SpringBootLogApplicationTests [main] Started SpringBootLogApplicationTests in 1.091 seconds (JVM running for 1.862)
[ERROR] 2023-02-28 18:32:27 io.github.jihch.SpringBootLogApplicationTests [main] error
[WARN ] 2023-02-28 18:32:27 io.github.jihch.SpringBootLogApplicationTests [main] warn
[INFO ] 2023-02-28 18:32:27 io.github.jihch.SpringBootLogApplicationTests [main] info
[DEBUG] 2023-02-28 18:32:27 io.github.jihch.SpringBootLogApplicationTests [main] debug
[TRACE] 2023-02-28 18:32:27 io.github.jihch.SpringBootLogApplicationTests [main] trace
[INFO ] 2023-02-28 18:32:27 io.github.jihch.SpringBootLogApplicationTests [main] log4j 2 info
[INFO ] 2023-02-28 18:32:27 org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor [SpringContextShutdownHook] Shutting down ExecutorService 'applicationTaskExecutor'
Process finished with exit code 0