docs/Spring全家桶/SpringBoot/SpringBoot中的日志管理.md
Spring Bootڲ־ʹ Commons Logging Եײ־ʵֱֿš Ϊ Java Util Logging Log4j2 Logback ṩĬá ÿһ£¼loggerԤΪʹÿ̨Ҳѡļ
Ĭ£ʹ StarterĬʹLogback ʵLogback·ҲڣȷʹJava Util LoggingCommons LoggingLog4JSLF4Jⶼȷ
| кܶJava־ܡ бܻң벻Ҫġ һ˵㲻Ҫı־Spring BootĬֵͺܺá |
|---|
| ӦóһservletӦ÷ʱJava Util Logging APIִе־ᱻ͵Ӧó־С ԷֹѾӦóִе־Ӧó־С |
|---|
Spring BootĬϵ־ʽӡ
2023-03-03T21:18:18.827+08:00 INFO 19388 --- [ main] o.s.b.d.f.s.MyApplication : Starting MyApplication using Java 17 with PID 19388 (/opt/apps/myapp.jar started by myuser in /opt/apps/) 2023-03-03T21:18:18.834+08:00 INFO 19388 --- [ main] o.s.b.d.f.s.MyApplication : No active profile set, falling back to 1 default profile: "default" 2023-03-03T21:18:20.439+08:00 INFO 19388 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http) 2023-03-03T21:18:20.461+08:00 INFO 19388 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat] 2023-03-03T21:18:20.461+08:00 INFO 19388 --- [ main] o.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/10.1.5] 2023-03-03T21:18:20.600+08:00 INFO 19388 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext 2023-03-03T21:18:20.602+08:00 INFO 19388 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1685 ms 2023-03-03T21:18:21.078+08:00 INFO 19388 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path '' 2023-03-03T21:18:21.093+08:00 INFO 19388 --- [ main] o.s.b.d.f.s.MyApplication : Started MyApplication in 2.998 seconds (process running for 3.601)
Ŀ¡
DateʱTimeȷ룬
־: ERROR, WARN, INFO, DEBUG, TRACE.
ID
һ --- ָʵ־ϢĿʼ
߳ƣڷУڿ̨ܻᱻضϣ
¼ƣͨԴƣͨд
־Ϣ
Logbackû FATAL ӳ䵽 ERROR |
|---|
Ĭ£־ ERROR``WARN INFO Ϣ̨ Ҳͨ --debug ־Ӧó debug ģʽ
$ java -jar myapp.jar --debug
Ҳ application.properties ָ debug=true |
|---|
debugģʽʱһЩļ¼ǶʽHibernateSpring BootΪϢ debugģʽζŽӦóΪ DEBUG ¼Ϣ
⣬ͨӦóʱʹ --trace ־ application.properties ʹ trace=true trace ģʽ ԶһЩļ¼ǶʽHibernate schemaɺSpringϣиټ¼
ն֧ANSIͻʹòɫĶ Խ spring.output.ansi.enabled Ϊ ֵֵ֧ԸԶ⡣
ɫͨʹ %clr תؼõġ ʽУת־ɫʾ
%clr(%5p)
±־ɫӳϵ
<colgroup><col><col></colgroup> | ־ | ɫ | | --- | --- | | `FATAL` | | | `ERROR` | | | `WARN` | | | `INFO` | | | `DEBUG` | | | `TRACE` | |⣬ҲͨΪתṩһѡָӦʹõɫʽ 磬ҪʹıΪɫʹá
%clr(%d{yyyy-MM-dd'T'HH:mm:ss.SSSXXX}){yellow}
֧ɫʽ
blue
cyan
faint
green
magenta
red
yellow
Ĭ£Spring Bootֻ̨¼־д־ļ ڿ̨֮д־ļҪ logging.file.name logging.file.path ԣ磬 application.properties У
±ʾ logging.* αһʹá
־ļڴﵽ10MBʱͻֻ̨һĬ»¼ ERROR WARN INFO Ϣ
־Զʵʵ־ʩ ˣضԣLogback logback.configurationFile spring Boot |
|---|
ʹLogbackʹ application.properties application.yaml ļ־ֻá ־ϵͳ㽫ҪԼֱֻã磬ʹLog4J2ôһ log4j2.xml log4j2-spring.xml ļ
ֻ֧ԡ
<colgroup><col><col></colgroup> | | ˵ | | --- | --- | | `logging.logback.rollingpolicy.file-name-pattern` | ڴ־鵵ļģʽ | | `logging.logback.rollingpolicy.clean-history-on-start` | ӦóʱǷ־鵵 | | `logging.logback.rollingpolicy.max-file-size` | ־ļ鵵ǰߴ磨ļﵽͻ鵵 | | `logging.logback.rollingpolicy.total-size-cap` | ־ڱɾǰߴ磨鵵ļռôССᱻɾ | | `logging.logback.rollingpolicy.max-history` | ҪĹ鵵־ļĬΪ7 |ֵ֧־ϵͳͨʹ logging.level.<logger-name>=<level> Spring Environment磬 application.properties־ level TRACE, DEBUG, INFO, WARN, ERROR, FATAL, OFF ֮һ root ¼loggerļͨ logging.level.root á
ʾ application.properties DZڵ־á
Properties
Yaml
logging.level.root=warn
logging.level.org.springframework.web=debug
logging.level.org.hibernate=error
Ҳʹû־ 磬LOGGING_LEVEL_ORG_SPRINGFRAMEWORK_WEB=DEBUG org.springframework.web Ϊ DEBUG
ֻڰ־ ڿɰǽתΪСдĸԲַʽΪ־ ҪΪһ־ʹSPRING_APPLICATION_JSON |
|---|
ܹص־¼飬Աͬʱǽãͨá 磬ܾı __ Tomcatصļ¼ļ¼𣬵㲻סİ
Ϊ˰⣬Spring BootSpring Environment ж־顣 磬ͨ application.properties м tomcat group
Properties
Yaml
logging.group.tomcat=org.apache.catalina,org.apache.coyote,org.apache.tomcat
һúͿһдıloggerļ
Properties
Yaml
logging.level.tomcat=trace
Spring BootԤ־飬Կ伴á
<colgroup><col><col></colgroup> | | еlogger | | --- | --- | | web | `org.springframework.core.codec`, `org.springframework.http`, `org.springframework.web`, `org.springframework.boot.actuate.endpoint.web`, `org.springframework.boot.web.servlet.ServletContextInitializerBeans` | | sql | `org.springframework.jdbc.core`, `org.hibernate.SQL`, `org.jooq.tools.LoggerListener` |ΪӦóֹʱͷ־ԴṩһShutdown HookJVM˳ʱ־ϵͳ ӦówarļʽģShutdown HookԶעᡣ ӦóиӵIJνṹShutdown Hook ܣùػӣоײ־ϵͳֱṩѡ 磬Logbackṩ context selectorsÿ¼Լб ʹ logging.register-shutdown-hook Shutdown Hook Ϊ false עᡣ application.properties application.yaml ļøԡ
Properties
Yaml
logging.register-shutdown-hook=false
־ϵͳͨclasspathϰʵĿҿͨclasspathĸĿ¼» Spring Environment ָλṩһʵļһƣ logging.config
ͨʹ org.springframework.boot.logging.LoggingSystem ϵͳԣǿSpring Bootʹض־ϵͳ ֵӦ LoggingSystem ʵֵȫ Ҳͨʹ none ֵȫSpring Boot־á
S־ڴ ApplicationContext ֮ǰʼģԲܴSpring @Configuration ļе @PropertySources ־ ı־ϵͳȫͣΨһͨSystem properties |
|---|
־ϵͳļ
<colgroup><col><col></colgroup> | ־ϵͳ | ļ | | --- | --- | | Logback | `logback-spring.xml`, `logback-spring.groovy`, `logback.xml` `logback.groovy` | | Log4j2 | `log4j2-spring.xml` `log4j2.xml` | | JDK (Java Util Logging) | `logging.properties` |ڿܵ£ǽʹ -spring ־ã磬 logback-spring.xml logback.xml ʹñλãSpringȫ־ʼ |
|---|
| "ִеjar "ʱJava Util LoggingһЩ֪⣬ᵼ⡣ ܵĻǽڴ "ִеjar" ʱʹ |
|---|
Ϊ˰ƣһЩԴSpring Environment תƵSystem properties±ʾ
| Spring Environment | System Property | ע |
|---|---|---|
logging.exception-conversion-word | LOG_EXCEPTION_CONVERSION_WORD | ¼쳣ʱʹõתʡ |
logging.file.name | LOG_FILE | ˣĬϵ־С |
logging.file.path | LOG_PATH | ˣĬϵ־С |
logging.pattern.console | CONSOLE_LOG_PATTERN | ڿ̨stdoutʹõ־ģʽ |
logging.pattern.dateformat | LOG_DATEFORMAT_PATTERN | date ʽ. |
logging.charset.console | CONSOLE_LOG_CHARSET | ̨־ַ롣 |
logging.threshold.console | CONSOLE_LOG_THRESHOLD | ڿ̨־¼־ |
logging.pattern.file | FILE_LOG_PATTERN | Ҫļʹõ־ģʽ LOG_FILE ã |
logging.charset.file | FILE_LOG_CHARSET | ļ־ַ루 LOG_FILE ã |
logging.threshold.file | FILE_LOG_THRESHOLD | ļ־¼־ |
logging.pattern.level | LOG_LEVEL_PATTERN | Ⱦ־ʱʹõĸʽĬΪ %5p |
PID | PID | ǰĽID |
ʹLogbackҲᱻתơ
| Spring Environment | System Property | ע |
|---|---|---|
logging.logback.rollingpolicy.file-name-pattern | LOGBACK_ROLLINGPOLICY_FILE_NAME_PATTERN | ־ļģʽĬΪ ${LOG_FILE}.%d{yyyy-MM-dd}.%i.gz |
logging.logback.rollingpolicy.clean-history-on-start | LOGBACK_ROLLINGPOLICY_CLEAN_HISTORY_ON_START | Ƿʱ鵵־ļ |
logging.logback.rollingpolicy.max-file-size | LOGBACK_ROLLINGPOLICY_MAX_FILE_SIZE | ־ļС |
logging.logback.rollingpolicy.total-size-cap | LOGBACK_ROLLINGPOLICY_TOTAL_SIZE_CAP | Ҫ־ݵܴС |
logging.logback.rollingpolicy.max-history | LOGBACK_ROLLINGPOLICY_MAX_HISTORY | Ҫ鵵־ļ |
ֵ֧־ϵͳڽļʱԴ System properties лȡԡ Ӽ spring-boot.jar еĬá
־ʹռλӦʹSpring Bootǵײܵ ֵעǣʹLogbackӦʹ : ΪĬֵ֮ķָʹ :- |
|---|
ֻͨ LOG_LEVEL_PATTERN ʹLogback logging.pattern.level ־MDCʱݡ 磬ʹ logging.pattern.level=user:%X{user} %5p ôĬϵ־ʽһ "user" MDCĿڵĻʾ 2019-08-30 12:30:04.031 user:someone INFO 22174 --- [ nio-8080-exec-0] demo.ControllerHandling authenticated request |
|---|
Spring BootһЩLogbackչиá logback-spring.xml ļʹЩչ
Ϊ logback.xml ļأ㲻ʹչ Ҫʹ logback-spring.xml ߶һ logging.config ԡ |
|---|
| չ Logbackɨ һʹá ͼļĻᵼµĴ¼ |
|---|
ERROR in ch.qos.logback.core.joran.spi.Interpreter@4:71 - no applicable action for [springProperty], current ElementPath is [[configuration][springProperty]] ERROR in ch.qos.logback.core.joran.spi.Interpreter@4:71 - no applicable action for [springProfile], current ElementPath is [[configuration][springProfile]]
<springProfile> ǩԸݻSpringļѡԵذųõIJ֣ ֧ <configuration> Ԫصκεط ʹ name ָõļ <springProfile> ǩһļƣ staging һļʽ ļʽӵļ production & (eu-central | eu-west) 鿴 Spring ܲοָ ˽ϸڡ бʾļ
<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>
<springProperty> ǩԷ Spring Environment еԣԱLogbackʹá Logbackз application.properties ļеֵá ñǩĹʽLogbackı <property> ǩơ Ȼ㲻ֱָһ value ָԵ source Environment Ҫ local Χĵط洢ԣʹ scope ԡ ҪһֵĬֵһû Environment ãʹ defaultValue ԡ ʾιԱLogbackʹá
<springProperty scope="context" name="fluentHost" source="myapp.fluentd.host"
defaultValue="localhost"/>
<remoteHost>${fluentHost}</remoteHost>
...
source kebabָ my.property-name ȻԿͨʹÿɵĹӵ Environment С |
|---|
Spring BootһЩLog4j2չиáκ log4j2-spring.xml ļʹЩչ
Ϊ log4j2.xml ļأ㲻ʹչҪʹ log4j2-spring.xml ߶һ logging.config ԡ |
|---|
ЩչȡLog4Jṩ Spring Boot֧ ӦȷĹв org.apache.logging.log4j:log4j-spring-boot ģ顣 |
|---|
<SpringProfile> ǩԸݻSpringļѡԵذųõIJ֡ļֱ֧ <Configuration> Ԫصκεطʹ name ָĸļá <SpringProfile> ǩһļƣ stagingһļʽ ļʽӵļ production & (eu-central | eu-west)鿴 Springܲοָ ˽ϸڡ бʾļ
<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>
Log4j2Spring Environment еԣʹ spring: ǰ Log4j2з application.properties ļеֵá
ʾһΪ applicationName Log4j2ԣSpring Environment жȡ spring.application.name
<Properties>
<Property name="applicationName">${spring:spring.application.name}</Property>
</Properties>
ѯkeyӦkebabfָ my.property-name |
|---|
Log4j2֧һЩ System PropertiesøĿ磬log4j2.skipJansi ϵͳԿ ConsoleAppender ǷWindowsϳʹ Jansi
Log4j2 ʼصϵͳԶԴSpring Environment лá磬 application.properties ļ log4j2.skipJansi=false ConsoleAppender WindowsʹJansi
ֻеϵͳԣsystem propertiesͲϵͳڼصֵʱŻῼSpring Environment |
|---|
Log4j2ʼڼصϵͳԲSpring Environment磬Log4j2ѡĬLog4j2ʵֵ Spring Environment ֮ǰʹõġ |
|---|