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>
		<triggeringPolicy
			class="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>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值