查看“Spring Boot 日志配置”的源代码
←
Spring Boot 日志配置
跳到导航
跳到搜索
因为以下原因,您没有权限编辑本页:
您所请求的操作仅限于该用户组的用户使用:
用户
您可以查看和复制此页面的源代码。
https://www.bilibili.com/video/BV1iJ411H74S?p=37 === 示例 无配置文件 默认使用 SLF4J 日志门面 + Logback 日志实现 === ==== pom.xml ==== <syntaxhighlight lang="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> </syntaxhighlight> ==== SpringBootLogApplication.java ==== <syntaxhighlight lang="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); } } </syntaxhighlight> ==== SpringBootLogApplicationTests.java ==== <syntaxhighlight lang="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"); } } </syntaxhighlight><syntaxhighlight lang="console"> 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' </syntaxhighlight> === 示例 无配置文件 使用 Log4j 2记录日志也还是会经由桥接器使用 SLF4J 日志门面 + Logback 日志实现 === ==== SpringBootLogApplicationTests.java ==== <syntaxhighlight lang="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"); } } </syntaxhighlight><syntaxhighlight lang="console"> 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 </syntaxhighlight>观察控制台的输出可以发现使用 Log4j 2 的 API 输出和 SLF4J API 输出 pattern 是一样的,因为最终还是经由桥接器调用 SLF4J 作为日志门面 + Logback 作为日志实现进行日志记录 === 示例 指定自定义 logger 对象日志级别 === ==== application.properties ==== <syntaxhighlight lang="properties"> # 指定自定义 logger 对象日志级别 logging.level.io.github.jihch=trace </syntaxhighlight> ==== SpringBootLogApplication.java ==== 不变 ==== SpringBootLogApplicationTests.java ==== 不变<syntaxhighlight lang="console"> 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 </syntaxhighlight>观察控制台输出,可以看到对比前两次,这次还输出了 debug 和 trace === 示例 指定控制台输出日志消息格式 === ==== application.properties ==== <syntaxhighlight lang="console"> # 指定自定义 logger 对象日志级别 logging.level.io.github.jihch=trace # 指定控制台输出消息格式 logging.pattern.console=[%-5level] %d{yyyy-MM-dd HH:mm:ss} %c [%thread]===== %msg%n </syntaxhighlight>其他不变<syntaxhighlight lang="console"> 18:38:10.595 [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:38:11 io.github.jihch.SpringBootLogApplicationTests [main]===== Starting SpringBootLogApplicationTests on SK-20210414CLPG with PID 49856 (started by Administrator in E:\record\2023\2\27\spring-boot-log) [DEBUG] 2023-02-28 18:38:11 io.github.jihch.SpringBootLogApplicationTests [main]===== Running with Spring Boot v2.2.2.RELEASE, Spring v5.2.2.RELEASE [INFO ] 2023-02-28 18:38:11 io.github.jihch.SpringBootLogApplicationTests [main]===== No active profile set, falling back to default profiles: default [INFO ] 2023-02-28 18:38:11 org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor [main]===== Initializing ExecutorService 'applicationTaskExecutor' [INFO ] 2023-02-28 18:38:11 io.github.jihch.SpringBootLogApplicationTests [main]===== Started SpringBootLogApplicationTests in 1.122 seconds (JVM running for 1.803) [ERROR] 2023-02-28 18:38:12 io.github.jihch.SpringBootLogApplicationTests [main]===== error [WARN ] 2023-02-28 18:38:12 io.github.jihch.SpringBootLogApplicationTests [main]===== warn [INFO ] 2023-02-28 18:38:12 io.github.jihch.SpringBootLogApplicationTests [main]===== info [DEBUG] 2023-02-28 18:38:12 io.github.jihch.SpringBootLogApplicationTests [main]===== debug [TRACE] 2023-02-28 18:38:12 io.github.jihch.SpringBootLogApplicationTests [main]===== trace [INFO ] 2023-02-28 18:38:12 io.github.jihch.SpringBootLogApplicationTests [main]===== log4j 2 info [INFO ] 2023-02-28 18:38:12 org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor [SpringContextShutdownHook]===== Shutting down ExecutorService 'applicationTaskExecutor' Process finished with exit code 0 </syntaxhighlight> === 示例 配置日志输出到文件 === ==== application.properties ==== <syntaxhighlight lang="properties"> # 指定自定义 logger 对象日志级别 logging.level.io.github.jihch=trace # 指定控制台输出消息格式 logging.pattern.console=[%-5level] %d{yyyy-MM-dd HH:mm:ss} %c [%thread]===== %msg%n # 指定存放日志文件的具体路径 logging.file=/logs/springboot.log # 指定日志文件消息格式 logging.pattern.file=[%-5level] %d{yyyy-MM-dd HH:mm:ss} %c [%thread]===== %msg%n </syntaxhighlight>目录 E:\logs 下出现 springboot.log 日志文件<syntaxhighlight lang="console"> [INFO ] 2023-02-28 19:11:50 io.github.jihch.SpringBootLogApplicationTests [main]===== Starting SpringBootLogApplicationTests on SK-20210414CLPG with PID 26692 (started by Administrator in E:\record\2023\2\27\spring-boot-log) [DEBUG] 2023-02-28 19:11:50 io.github.jihch.SpringBootLogApplicationTests [main]===== Running with Spring Boot v2.2.2.RELEASE, Spring v5.2.2.RELEASE [INFO ] 2023-02-28 19:11:50 io.github.jihch.SpringBootLogApplicationTests [main]===== No active profile set, falling back to default profiles: default [INFO ] 2023-02-28 19:11:50 org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor [main]===== Initializing ExecutorService 'applicationTaskExecutor' [INFO ] 2023-02-28 19:11:51 io.github.jihch.SpringBootLogApplicationTests [main]===== Started SpringBootLogApplicationTests in 1.184 seconds (JVM running for 1.867) [ERROR] 2023-02-28 19:11:51 io.github.jihch.SpringBootLogApplicationTests [main]===== error [WARN ] 2023-02-28 19:11:51 io.github.jihch.SpringBootLogApplicationTests [main]===== warn [INFO ] 2023-02-28 19:11:51 io.github.jihch.SpringBootLogApplicationTests [main]===== info [DEBUG] 2023-02-28 19:11:51 io.github.jihch.SpringBootLogApplicationTests [main]===== debug [TRACE] 2023-02-28 19:11:51 io.github.jihch.SpringBootLogApplicationTests [main]===== trace [INFO ] 2023-02-28 19:11:51 io.github.jihch.SpringBootLogApplicationTests [main]===== log4j 2 info [INFO ] 2023-02-28 19:11:51 org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor [SpringContextShutdownHook]===== Shutting down ExecutorService 'applicationTaskExecutor' </syntaxhighlight>但其实 logging.file 是已经注解了 @Deprecated 的属性,使用它的替代方案再进行测试<syntaxhighlight lang="properties"> # 指定自定义 logger 对象日志级别 logging.level.io.github.jihch=trace # 指定控制台输出消息格式 logging.pattern.console=[%-5level] %d{yyyy-MM-dd HH:mm:ss} %c [%thread]===== %msg%n # 指定存放日志文件的具体路径 # logging.file=/logs/springboot.log #已经被废弃的属性 # 指定日志文件存放的目录,默认的文件名 spring.log logging.file.path=/logs/springboot/ # 指定日志文件消息格式 logging.pattern.file=[%-5level] %d{yyyy-MM-dd HH:mm:ss} %c [%thread]===== %msg%n </syntaxhighlight>发现在 E:\logs\springboot\ 目录下出现了 spring.log,内容和之前一样 如果既配置了 logging.file 又配置了 logging.file.path,那么 logging.file 生效 === 指定配置 === 给类路径下放上每个日志框架自己的配置文件,Spring Boot 就不使用默认配置了 {| class="wikitable" !日志框架 !配置文件 |- |Logback |logback-spring.xml,logback.xml |- |Log4j 2 |log4j2-spring.xml,log4j2.xml |- |JUL |logging.properties |} logback.xml 直接就会被日志框架识别 === 示例 使用 logback.xml 进行日志配置 === ==== logback.xml ==== <syntaxhighlight lang="xml"> <?xml version="1.0" encoding="UTF-8" ?> <configuration> <!-- 配置集中管理属性 可以直接该属性的 value 值 格式:${name} --> <property name="pattern" value="[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} %c %M %L [%thread] -------- %m%n"></property> <!-- 控制台日志输出的 appender --> <appender name="console" class="ch.qos.logback.core.ConsoleAppender"> <!-- 控制输出流对象 默认 System.out 改为 System.err --> <target>System.err</target> <!-- 日志消息格式配置 --> <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder"> <pattern>${pattern}</pattern> </encoder> </appender> <!-- 自定义 logger 对象 additivity="false" 自定义 logger 对象是否继承 rootLogger --> <logger name="io.github.jihch" level="info" additivity="false"> <appender-ref ref="console"/> </logger> </configuration> </syntaxhighlight>其他不变<syntaxhighlight lang="console"> . ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v2.2.2.RELEASE) [INFO ] 2023-02-28 19:30:44.324 io.github.jihch.SpringBootLogApplicationTests logStarting 55 [main] -------- Starting SpringBootLogApplicationTests on SK-20210414CLPG with PID 49888 (started by Administrator in E:\record\2023\2\27\spring-boot-log) [DEBUG] 2023-02-28 19:30:44.327 io.github.jihch.SpringBootLogApplicationTests logStarting 56 [main] -------- Running with Spring Boot v2.2.2.RELEASE, Spring v5.2.2.RELEASE [INFO ] 2023-02-28 19:30:44.327 io.github.jihch.SpringBootLogApplicationTests logStartupProfileInfo 651 [main] -------- No active profile set, falling back to default profiles: default [INFO ] 2023-02-28 19:30:45.275 io.github.jihch.SpringBootLogApplicationTests logStarted 61 [main] -------- Started SpringBootLogApplicationTests in 1.106 seconds (JVM running for 1.853) [ERROR] 2023-02-28 19:30:45.468 io.github.jihch.SpringBootLogApplicationTests contextLoads 18 [main] -------- error [WARN ] 2023-02-28 19:30:45.468 io.github.jihch.SpringBootLogApplicationTests contextLoads 19 [main] -------- warn [INFO ] 2023-02-28 19:30:45.468 io.github.jihch.SpringBootLogApplicationTests contextLoads 20 [main] -------- info [DEBUG] 2023-02-28 19:30:45.468 io.github.jihch.SpringBootLogApplicationTests contextLoads 21 [main] -------- debug [TRACE] 2023-02-28 19:30:45.468 io.github.jihch.SpringBootLogApplicationTests contextLoads 22 [main] -------- trace [INFO ] 2023-02-28 19:30:45.469 io.github.jihch.SpringBootLogApplicationTests contextLoads 26 [main] -------- log4j 2 info Process finished with exit code 0 </syntaxhighlight>Spring Boot 里 Logback 的配置文件不光可以叫 logback.xml,还可以叫 logback-spring.xml,也可以被 Spring Boot 加载 === 示例 重命名 logback.xml 为 logback-spring.xml === 观察发现输出的日志和之前一样,既然输出的日志是一样的,那为什么默认可以接受 logback.xml,也可以接受 logback-spring.xml 呢? Spring Boot 之所以同时支持 logback.xml 和 logback-spring.xml 作为日志配置文件,是为了提供更灵活的配置选项。 在传统的 Spring 应用程序中,通常使用 logback.xml 或者其他日志框架的 XML 配置文件来配置日志记录器。 但是,这种方法有一个缺点,就是无法利用 Spring 的依赖注入(DI)和环境抽象化功能。 Spring Boot 提供了一种新的方法来配置日志记录器,即 logback-spring.xml。这个文件与 logback.xml 有些不同,它允许使用 Spring 的 DI 和环境抽象化功能,以及使用 Spring 的属性文件和 YAML 配置文件来覆盖默认的日志配置。 因此,如果你想使用传统的 logback.xml 配置文件,那么 Spring Boot 也可以支持它。 但是,如果你想使用 Spring 的 DI 和环境抽象化功能来配置日志记录器,那么你可以选择使用 logback-spring.xml。 最后需要注意的是,如果同时存在 logback.xml 和 logback-spring.xml 配置文件,Spring Boot 将首选使用 logback-spring.xml 文件作为日志配置文件。 这是因为 logback-spring.xml 可以利用 Spring 的 DI 和环境抽象化功能,使日志记录器的配置更加灵活。 如果使用的配置文件名是 logback-spring.xml,那么默认是被 Spring Boot 框架支持、解析的,就可以自定义日志消息的格式: === 示例 使用 logback-spring.xml 配置根据环境应用不同配置 === ==== logback-spring.xml ==== <syntaxhighlight lang="xml"> <?xml version="1.0" encoding="UTF-8" ?> <configuration> <!-- 配置集中管理属性 可以直接该属性的 value 值 格式:${name} --> <property name="pattern" value="[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} %c %M %L [%thread] -------- %m%n"></property> <!-- 控制台日志输出的 appender --> <appender name="console" class="ch.qos.logback.core.ConsoleAppender"> <!-- 控制输出流对象 默认 System.out 改为 System.err --> <target>System.err</target> <!-- 日志消息格式配置 --> <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder"> <springProfile name="dev"> <pattern>${pattern}</pattern> </springProfile> <springProfile name="pro"> <pattern>[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} %c %M %L [%thread] xxxxxxxx %m%n</pattern> </springProfile> </encoder> </appender> <!-- 自定义 logger 对象 additivity="false" 自定义 logger 对象是否继承 rootLogger --> <logger name="io.github.jihch" level="info" additivity="false"> <appender-ref ref="console"/> </logger> </configuration> </syntaxhighlight> ==== application.properties ==== <syntaxhighlight lang="properties"> # 指定自定义 logger 对象日志级别 logging.level.io.github.jihch=trace # 指定控制台输出消息格式 logging.pattern.console=[%-5level] %d{yyyy-MM-dd HH:mm:ss} %c [%thread]===== %msg%n # 指定存放日志文件的具体路径 # logging.file=/logs/springboot.log #已经被废弃的属性 # 指定日志文件存放的目录,默认的文件名 spring.log logging.file.path=/logs/springboot/ # 指定日志文件消息格式 logging.pattern.file=[%-5level] %d{yyyy-MM-dd HH:mm:ss} %c [%thread]===== %msg%n # 指定项目使用的具体环境 spring.profiles.active=dev </syntaxhighlight><syntaxhighlight lang="console"> …… [INFO ] 2023-02-28 22:15:24.693 io.github.jihch.SpringBootLogApplicationTests logStarting 55 [main] -------- Starting SpringBootLogApplicationTests on SK-20210414CLPG with PID 30644 (started by Administrator in E:\record\2023\2\27\spring-boot-log) [DEBUG] 2023-02-28 22:15:24.695 io.github.jihch.SpringBootLogApplicationTests logStarting 56 [main] -------- Running with Spring Boot v2.2.2.RELEASE, Spring v5.2.2.RELEASE [INFO ] 2023-02-28 22:15:24.695 io.github.jihch.SpringBootLogApplicationTests logStartupProfileInfo 655 [main] -------- The following profiles are active: dev [INFO ] 2023-02-28 22:15:25.624 io.github.jihch.SpringBootLogApplicationTests logStarted 61 [main] -------- Started SpringBootLogApplicationTests in 1.203 seconds (JVM running for 1.885) [ERROR] 2023-02-28 22:15:25.821 io.github.jihch.SpringBootLogApplicationTests contextLoads 18 [main] -------- error [WARN ] 2023-02-28 22:15:25.821 io.github.jihch.SpringBootLogApplicationTests contextLoads 19 [main] -------- warn [INFO ] 2023-02-28 22:15:25.822 io.github.jihch.SpringBootLogApplicationTests contextLoads 20 [main] -------- info [DEBUG] 2023-02-28 22:15:25.822 io.github.jihch.SpringBootLogApplicationTests contextLoads 21 [main] -------- debug [TRACE] 2023-02-28 22:15:25.822 io.github.jihch.SpringBootLogApplicationTests contextLoads 22 [main] -------- trace [INFO ] 2023-02-28 22:15:25.823 io.github.jihch.SpringBootLogApplicationTests contextLoads 26 [main] -------- log4j 2 info </syntaxhighlight> ==== 修改 application.properties ==== <syntaxhighlight lang="properties"> …… # 指定项目使用的具体环境 spring.profiles.active=pro </syntaxhighlight>再运行:<syntaxhighlight lang="console"> …… [INFO ] 2023-02-28 22:18:52.498 io.github.jihch.SpringBootLogApplicationTests logStarting 55 [main] xxxxxxxx Starting SpringBootLogApplicationTests on SK-20210414CLPG with PID 28572 (started by Administrator in E:\record\2023\2\27\spring-boot-log) [DEBUG] 2023-02-28 22:18:52.499 io.github.jihch.SpringBootLogApplicationTests logStarting 56 [main] xxxxxxxx Running with Spring Boot v2.2.2.RELEASE, Spring v5.2.2.RELEASE [INFO ] 2023-02-28 22:18:52.499 io.github.jihch.SpringBootLogApplicationTests logStartupProfileInfo 655 [main] xxxxxxxx The following profiles are active: pro [INFO ] 2023-02-28 22:18:53.411 io.github.jihch.SpringBootLogApplicationTests logStarted 61 [main] xxxxxxxx Started SpringBootLogApplicationTests in 1.193 seconds (JVM running for 1.894) [ERROR] 2023-02-28 22:18:53.594 io.github.jihch.SpringBootLogApplicationTests contextLoads 18 [main] xxxxxxxx error [WARN ] 2023-02-28 22:18:53.595 io.github.jihch.SpringBootLogApplicationTests contextLoads 19 [main] xxxxxxxx warn [INFO ] 2023-02-28 22:18:53.595 io.github.jihch.SpringBootLogApplicationTests contextLoads 20 [main] xxxxxxxx info [DEBUG] 2023-02-28 22:18:53.595 io.github.jihch.SpringBootLogApplicationTests contextLoads 21 [main] xxxxxxxx debug [TRACE] 2023-02-28 22:18:53.595 io.github.jihch.SpringBootLogApplicationTests contextLoads 22 [main] xxxxxxxx trace [INFO ] 2023-02-28 22:18:53.596 io.github.jihch.SpringBootLogApplicationTests contextLoads 26 [main] xxxxxxxx log4j 2 info </syntaxhighlight>
返回至
Spring Boot 日志配置
。
导航菜单
个人工具
登录
名字空间
页面
讨论
变种
视图
阅读
查看源代码
查看历史
更多
搜索
导航
首页
Spring Boot 2 零基础入门
Spring Cloud
Spring Boot
设计模式之禅
VUE
Vuex
Maven
算法
技能树
Wireshark
IntelliJ IDEA
ElasticSearch
VirtualBox
软考
正则表达式
程序员精讲
软件设计师精讲
初级程序员 历年真题
C
SQL
Java
FFmpeg
Redis
Kafka
MySQL
Spring
Docker
JMeter
Apache
Linux
Windows
Git
ZooKeeper
设计模式
Python
MyBatis
软件
数学
PHP
IntelliJ IDEA
CS基础知识
网络
项目
未分类
MediaWiki
镜像
问题
健身
国债
英语
烹饪
常见术语
MediaWiki帮助
工具
链入页面
相关更改
特殊页面
页面信息