SpringBoot彩色日志配置方式

SpringBoot彩色日志配置

logback 顏色

logback 顏色配置

配置

logging.level.root=INFO
logging.pattern.console=%green([%d{yyyy-MM-dd HH:mm:ss.SSS}]) %cyan(%thread) %highlight(%-5level) %magenta(%logger{36}) %yellow(%L) - %gray(%msg%n)

SpringBoot打印彩色日志

logback配置

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
	<!-- 文件輸出格式 -->
	<property name="PATTERN"
		value="%-12(%d{yyyy-MM-dd HH:mm:ss.SSS}) |-%-5level [%thread] %c [%L] -| %msg%n" />
	<conversionRule conversionWord="clr" converterClass="org.springframework.boot.logging.logback.ColorConverter" />
	<conversionRule conversionWord="wex" converterClass="org.springframework.boot.logging.logback.WhitespaceThrowableProxyConverter" />
	<conversionRule conversionWord="wEx" converterClass="org.springframework.boot.logging.logback.ExtendedWhitespaceThrowableProxyConverter" />
	<!-- 彩色日志格式 -->
	<property name="CONSOLE_LOG_PATTERN" value="${CONSOLE_LOG_PATTERN:-%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} %clr(${LOG_LEVEL_PATTERN:-%5p}) %clr(${PID:- }){magenta} %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}}"/>
	<!-- test文件路徑 -->
	<property name="TEST_FILE_PATH" value="d:/test.log" />
	<!-- pro文件路徑 -->
	<property name="PRO_FILE_PATH" value="/opt/test/log" />
	<!-- 開發環境 -->
	<springProfile name="dev">
		<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
			<encoder>
				<pattern>${CONSOLE_LOG_PATTERN}</pattern>
			</encoder>
		</appender>
		<logger name="com.sdyy" level="debug" />
		<root level="info">
			<appender-ref ref="CONSOLE" />
		</root>
	</springProfile>
	<!-- 測試環境 -->
	<springProfile name="test">
		<!-- 每天產生一個文件 -->
		<appender name="TEST-FILE"
			class="ch.qos.logback.core.rolling.RollingFileAppender">
			<!-- 文件路徑 -->
			<file>${TEST_FILE_PATH}</file>
			<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
				<!-- 文件名稱 -->
				<fileNamePattern>${TEST_FILE_PATH}/info.%d{yyyy-MM-dd}.log</fileNamePattern>
				<!-- 文件最大保存歷史數量 -->
				<MaxHistory>100</MaxHistory>
			</rollingPolicy>
			<layout class="ch.qos.logback.classic.PatternLayout">
				<pattern>${PATTERN}</pattern>
			</layout>
		</appender>
		<logger name="com.sdyy" level="debug" />
		<root level="info">
			<appender-ref ref="TEST-FILE" />
		</root>
	</springProfile>
	<!-- 生產環境 -->
	<springProfile name="prod">
		<appender name="PROD_FILE"
			class="ch.qos.logback.core.rolling.RollingFileAppender">
			<file>${PRO_FILE_PATH}</file>
			<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
				<fileNamePattern>${PRO_FILE_PATH}/warn.%d{yyyy-MM-dd}.log</fileNamePattern>
				<MaxHistory>100</MaxHistory>
			</rollingPolicy>
			<layout class="ch.qos.logback.classic.PatternLayout">
				<pattern>${PATTERN}</pattern>
			</layout>
		</appender>
		<root level="warn">
			<appender-ref ref="PROD_FILE" />
		</root>
	</springProfile>
</configuration>

總結

以上為個人經驗,希望能給大傢一個參考,也希望大傢多多支持WalkonNet。

推薦閱讀: