本篇文章主要介绍SpringBoot日志配置相关内容。方便大家更好的使用日志,为问题的排查和日志的维护提供便利。如果文章中有错误或不明确的地方,请大家望指正,谢谢!
代码:
Application.java
package com.wm8.logging; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
pom.xml
<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> <groupId>com.wm8</groupId> <artifactId>logging</artifactId> <version>1.0</version> <packaging>jar</packaging> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.3.2.RELEASE</version> </parent> <name>logging</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> </project>
代码启动后,日志输入如下
. ____ _ __ _ _ /\\ / ___‘_ __ _ _(_)_ __ __ _ \ \ \ ( ( )\___ | ‘_ | ‘_| | ‘_ \/ _` | \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ‘ |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v2.3.2.RELEASE) 2021-01-04 15:27:22.313 INFO 40548 --- [ main] com.wm8.logging.Application : Starting Application on XXX with PID 40548 (C:\tool\workspace\logging\target\classes started by XXX in C:\tool\workspace\logging) 2021-01-04 15:27:22.313 INFO 40548 --- [ main] com.wm8.logging.Application : No active profile set, falling back to default profiles: default 2021-01-04 15:27:24.426 INFO 40548 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http) 2021-01-04 15:27:24.458 INFO 40548 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat] 2021-01-04 15:27:24.458 INFO 40548 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.37] 2021-01-04 15:27:24.614 INFO 40548 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext 2021-01-04 15:27:24.614 INFO 40548 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 2053 ms 2021-01-04 15:27:24.973 INFO 40548 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService ‘applicationTaskExecutor‘ 2021-01-04 15:27:25.618 INFO 40548 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ‘‘ 2021-01-04 15:27:25.642 INFO 40548 --- [ main] com.wm8.logging.Application : Started Application in 4.217 seconds (JVM running for 5.007)
Spring Boot 内部使用Commons-logging来记录日志,方便于集成 Java Util Logging, Log4J2, 和Logback。默认使用logback。
日志格式介绍
日志级别:ERROR
WARN
INFO
DEBUG
TRACE
进程ID
一个---
分割器来区分实际日志消息的开始
线程名称:用方括号括起来
记录器名称: 源类名称
日志消息
日志输出到文件
Spring Boot 默认情况下只输出到console中,且日志级别为info级别。如果想要输出到文件中可以配置logging.file.name或者logging.flie.path属性
属性配置说明
logging.file.name | logging.file.path | Example | Description |
---|---|---|---|
(none) |
(none) |
只控制台输出 |
|
指定 |
(none) |
|
日志记录my.log文件. 值可以指定到精确位置比如:c:/logs/my.log。 如果没有路径,则输出到当前目录下 |
(none) |
指定的目录 |
|
写spring.log文件到指定的目录. 可以指定绝对路径或相对路径 |
当两个属性都配置时默认:logging.file.name优先.
添加:application.yml
logging: file: name: c:/logs/my.log
当日志文件达到10MB时,会自动压缩为gz包。效果如下:
文件更迭
该特性在2.4.1上支持 ,当前版本2.3.1不支持
如果使用logback,可以使用应用程序属性或者应用程序的yaml文件,来配置自己的更迭设置
支持的更迭策略属性
属性名 | 说明 |
---|---|
|
日志存档的文件名格式 |
|
是否在应用启动时进行日志存档清理 |
|
日志文件大小上限,达到后自动进行存档 |
|
存档的日志文件累计最大容量 |
|
保存多久的归档日志文件(默认7天) |
日志级别
logging.level.root=warn logging.level.org.springframework.web=debug logging.level.org.hibernate=error
包级别的日志配置
日志组
logging.group.tomcat=org.apache.catalina, org.apache.coyote, org.apache.tomcat
logging.level.tomcat=TRACE
对于多个包且日志级别相同的日志级别定义,此种方式更简洁。
Spring Boot 默认预定义了一些组
Name | Loggers |
---|---|
web |
|
sql |
|
自定义日志配置
Logging System | Customization |
---|---|
Logback |
|
Log4j2 |
|
JDK (Java Util Logging) |
|
根据不同的日志配置文件,会选用不同的日志系统。推荐使用-spring的日志配置文件
为了方便于自定义,一些属性从Spring 环境中转义到系统属性中
Spring Environment | System Property | Comments |
---|---|---|
|
|
The conversion word used when logging exceptions. |
|
|
Whether to clean the archive log files on startup (if LOG_FILE enabled). (Only supported with the default Logback setup.) |
|
|
If defined, it is used in the default log configuration. |
|
|
Maximum log file size (if LOG_FILE enabled). (Only supported with the default Logback setup.) |
|
|
Maximum number of archive log files to keep (if LOG_FILE enabled). (Only supported with the default Logback setup.) |
|
|
If defined, it is used in the default log configuration. |
|
|
Total size of log backups to be kept (if LOG_FILE enabled). (Only supported with the default Logback setup.) |
|
|
The log pattern to use on the console (stdout). (Only supported with the default Logback setup.) |
|
|
Appender pattern for log date format. (Only supported with the default Logback setup.) |
|
|
The log pattern to use in a file (if |
|
|
The format to use when rendering the log level (default |
|
|
Pattern for rolled-over log file names (default |
|
|
The current process ID (discovered if possible and when not already defined as an OS environment variable). |
Logback扩展
可以在不同profile下 定义属性
<springProfile name="staging"> <!-- configuration to be enabled when the "staging" profile is active --> </springProfile> <springProfile name="dev | staging"> <!-- configuration to be enabled when the "dev" or "staging" profiles are active --> </springProfile> <springProfile name="!production"> <!-- configuration to be enabled when the "production" profile is not active --> </springProfile>
声明一些系统属性,可以在Logback中使用,便于对日志输出格式进行扩展
<springProperty scope="context" name="fluentHost" source="myapp.fluentd.host" defaultValue="localhost"/> <appender name="FLUENT" class="ch.qos.logback.more.appenders.DataFluentAppender"> <remoteHost>${fluentHost}</remoteHost> ... </appender>
原文:https://www.cnblogs.com/9wan/p/14230195.html