SpringBoot Logback configuration, SpringBoot log configuration

SpringBoot Logback configuration, SpringBoot log configuration

 SpringBoot springProfile property configuration

 

================================

©Copyright Sweet Potato Yao March 26, 2018

http://fanshuyao.iteye.com/

 

Put logback-spring.xml or logback.xml under src/main/resources, and the springBoot logging framework will automatically identify the log configuration.

It is recommended to use the logback-spring.xml name , because you can use the advanced properties of springBoot (springProfile) : you can add the configuration of development mode, test mode, and production mode, which is conducive to multi-environment packaging.

 

<?xml version="1.0" encoding="UTF-8"?>
<configuration debug="false">

	<!--Define the storage address of log files Do not use relative paths in the configuration of LogBack-->
	<property name="LOG_HOME" value="/home" />
	<property name="pattern" value="%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n" />
	
	<!-- console output -->
	<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
		<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
			<!--Formatted output: %d means date, %thread means thread name, %-5level: level displays 5 characters width from left %msg: log message, %n is newline -->
			<pattern>${pattern}</pattern>
		</encoder>
	</appender>
	
	<!-- console output -->
	<!-- <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
		<layout class="ch.qos.logback.classic.PatternLayout">
			<springProfile name="dev">
				<Pattern>%date [%thread] %-5level %logger{80} - %msg%n</Pattern>
			</springProfile>
	    </layout>
	</appender> -->
	
	<!-- Generate log files on a daily basis-->
	<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
		<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
			<!--The file name of the log file output-->
			<FileNamePattern>${LOG_HOME}/TestWeb.log.%d{yyyy-MM-dd}.log</FileNamePattern>
			<!--Number of days to keep log files-->
			<MaxHistory>30</MaxHistory>
		</rollingPolicy>
		
		<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
			<!--Formatted output: %d means date, %thread means thread name, %-5level: level displays 5 characters width from left %msg: log message, %n is newline -->
			<pattern>${pattern}</pattern>
		</encoder>
		
		<!--Maximum size of log file-->
		<triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
			<MaxFileSize>10MB</MaxFileSize>
		</triggeringPolicy>
	</appender>

	<!-- log output level-->
	<root level="INFO">
		<appender-ref ref="STDOUT" />
	</root>
	
</configuration>

 

 

SpringProfile official description:

 

The <springProfile> tag lets you optionally include or exclude sections of configuration based on the active Spring profiles. Profile sections are supported anywhere within the <configuration> element. Use the name attribute to specify which profile accepts the configuration. Multiple profiles can be specified with a comma-separated list. The following listing shows three sample profiles:

<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>

 

example:

With springProfile configuration, the logback log configuration file must be named: logback-spring.xml

 

<springProfile name="dev">
		<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
			<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
				<!--Formatted output: %d means date, %thread means thread name, %-5level: level displays 5 characters width from left %msg: log message, %n is newline -->
				<pattern>---dev - %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern>
			</encoder>
		</appender>
</springProfile>
	
<springProfile name="test">
		<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
			<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
				<!--Formatted output: %d means date, %thread means thread name, %-5level: level displays 5 characters width from left %msg: log message, %n is newline -->
				<pattern>===test  %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern>
			</encoder>
		</appender>
</springProfile>
	
<springProfile name="zprod">
		<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
			<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
				<!--格式化输出:%d表示日期,%thread表示线程名,%-5level:级别从左显示5个字符宽度%msg:日志消息,%n是换行符-->
				<pattern>+++zprod  %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern>
			</encoder>
		</appender>
</springProfile>

 

然后在application.properties配置spring.profiles.active属性:

spring.profiles.active=zprod

 

 

================================

©Copyright 蕃薯耀 2018年3月26日

http://fanshuyao.iteye.com/

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326119927&siteId=291194637