當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
SpringBoot_日志-指定日志文件和日志Profile功能
生活随笔
收集整理的這篇文章主要介紹了
SpringBoot_日志-指定日志文件和日志Profile功能
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
這也只能改個別的配置,Springboot對日志到底是怎么樣的呢spring-boot-1.5.12.RELEASE.jar如果你用logback日志文件是什么樣的org.springframework.boot.logging.logback.base.xml<!--
Base logback configuration provided for compatibility with Spring Boot 1.1
--><included><include resource="org/springframework/boot/logging/logback/defaults.xml" /><property name="LOG_FILE" value="${LOG_FILE:-${LOG_PATH:-${LOG_TEMP:-${java.io.tmpdir:-/tmp}}}/spring.log}"/><include resource="org/springframework/boot/logging/logback/console-appender.xml" /><include resource="org/springframework/boot/logging/logback/file-appender.xml" /><root level="INFO"><appender-ref ref="CONSOLE" /><appender-ref ref="FILE" /></root>
</included>包含了defaults,默認要用這個,defaults里面定義了寫一些信息,包括CONSOLE_LOG_PATTERN,CONSOLE_LOG_PATTERN如果沒定義的情況下,我們看這個語法,${CONSOLE_LOG_PATTERN取出這個值,我們以前說過,沒有的話默認值是這一塊,-%clr(%d{yyyy-MM-dd HH:mm:ss.SSS})也包括了哪些東西用哪些級別,這塊也設置好了,<logger name="org.apache.catalina.startup.DigesterFactory" level="ERROR"/>
<logger name="org.apache.catalina.util.LifecycleBase" level="ERROR"/>
<logger name="org.apache.coyote.http11.Http11NioProtocol" level="WARN"/>
<logger name="org.apache.sshd.common.util.SecurityUtils" level="WARN"/>
<logger name="org.apache.tomcat.util.net.NioSelectorPool" level="WARN"/>
<logger name="org.crsh.plugin" level="WARN"/>
<logger name="org.crsh.ssh" level="WARN"/>
<logger name="org.eclipse.jetty.util.component.AbstractLifeCycle" level="ERROR"/>
<logger name="org.hibernate.validator.internal.util.Version" level="WARN"/>
<logger name="org.springframework.boot.actuate.autoconfigure.CrshAutoConfiguration" level="WARN"/>
<logger name="org.springframework.boot.actuate.endpoint.jmx" additivity="false">然后我們再來看base,這里有root-info的,就是我們之前說的,為什么我們在測試的時候,默認給我們指出info級別呢,因為root默認是info的,沒指定就跟著root來,我們在properties里面配置了文件名和文件路徑,其實都保存在org.springframework.boot.logging.LoggingApplicationListener里面了,而這個Listener對應LoggingSystemProperties,我們在Listener里配置的屬性最終就封裝到這/*** Utility to set system properties that can later be used by log configuration files.** @author Andy Wilkinson* @author Phillip Webb*/
class LoggingSystemProperties {static final String PID_KEY = LoggingApplicationListener.PID_KEY;static final String EXCEPTION_CONVERSION_WORD = LoggingApplicationListener.EXCEPTION_CONVERSION_WORD;static final String CONSOLE_LOG_PATTERN = LoggingApplicationListener.CONSOLE_LOG_PATTERN;static final String FILE_LOG_PATTERN = LoggingApplicationListener.FILE_LOG_PATTERN;static final String LOG_LEVEL_PATTERN = LoggingApplicationListener.LOG_LEVEL_PATTERN;private final Environment environment;然后在這會獲取到這些值,public void apply(LogFile logFile) {RelaxedPropertyResolver propertyResolver = RelaxedPropertyResolver.ignoringUnresolvableNestedPlaceholders(this.environment, "logging.");setSystemProperty(propertyResolver, EXCEPTION_CONVERSION_WORD,"exception-conversion-word");setSystemProperty(PID_KEY, new ApplicationPid().toString());setSystemProperty(propertyResolver, CONSOLE_LOG_PATTERN, "pattern.console");setSystemProperty(propertyResolver, FILE_LOG_PATTERN, "pattern.file");setSystemProperty(propertyResolver, LOG_LEVEL_PATTERN, "pattern.level");if (logFile != null) {logFile.applyToSystemProperties();}
}拿到propertyResolver屬性解析器,解析logging開頭的pattern,解析一大堆,解析完了以后呢,在配置文件里面就能夠用到這些值了,當時放的時候也是拿這個放的,static final String CONSOLE_LOG_PATTERN = LoggingApplicationListener.CONSOLE_LOG_PATTERN;取也是那這個取,<include resource="org/springframework/boot/logging/logback/console-appender.xml" />
<include resource="org/springframework/boot/logging/logback/file-appender.xml" />有console-appender.xml,有file-appender.xml,給控制臺輸出用什么,給文件輸出用什么,都幫我們定義好了,console-appender.xml<!--
Console appender logback configuration provided for import, equivalent to the programmatic
initialization performed by Boot
--><included><appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender"><encoder><pattern>${CONSOLE_LOG_PATTERN}</pattern><charset>utf8</charset></encoder></appender>
</included>用UTF8編碼,用${CONSOLE_LOG_PATTERN}這個輸出,如果沒有值,defaults里面不是規定了嗎,取CONSOLE_LOG_PATTERN,defaults里面可以規定,你沒規定就用默認的,包括文件這一塊file-appender.xml<!--
File appender logback configuration provided for import, equivalent to the programmatic
initialization performed by Boot
--><included><appender name="FILE"class="ch.qos.logback.core.rolling.RollingFileAppender"><encoder><pattern>${FILE_LOG_PATTERN}</pattern></encoder><file>${LOG_FILE}</file><rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy"><fileNamePattern>${LOG_FILE}.%i</fileNamePattern></rollingPolicy><triggeringPolicyclass="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy"><MaxFileSize>10MB</MaxFileSize></triggeringPolicy></appender>
</included>他用的是每天滾動的,RollingFileAppender,當我們這個文件超10MB以后,這個文件就會自動遞增,相當于每10MB分割一個文件,這一塊他都配好了,實際上這些配置我們覺得還不夠用,我們如果想要用到logback的其他高級功能,異步日志,自動歸檔了,我們想要用這些的時候,自己來寫日志的配置文件,怎么能用到自己的配置,其實也非常簡單,對于SpringBoot來說,我們可以參考官方文檔,專門有一個章節來說這個logging,https://docs.spring.io/spring-boot/docs/1.5.22.RELEASE/reference/html/boot-features-logging.html
每一個代表什么意思,日期,級別,線程id,還有名字一大堆
我們怎么定義自己的日志配置文件,非常簡單,如果你用logback,給路徑里面放一個logback-spring.xml,或者直接放logback.xml
<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>
?
總結
以上是生活随笔為你收集整理的SpringBoot_日志-指定日志文件和日志Profile功能的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: SpringBoot_日志-Spring
- 下一篇: SpringBoot_web开发-简介