logback常用配置

0

<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="false" scanPeriod="60 seconds" debug="false">

	<!-- 系统名称 -->
	<springProperty scope="context" name="system.name" source="spring.application.name" />
	<!-- 系统名称 -->
	<contextName>${system.name}</contextName>
	<!-- 日志目录 -->
	<property name="system.path" value="logs" />
	<!-- 异步日志队列长度 -->
	<property name="system.queue" value="2048" />
	<!-- 日志文件缓存大小 -->
	<property name="system.buffer" value="8192" />
	<!-- 日志保留时间 -->
	<property name="system.history" value="30" />
	<!-- 日志编码 -->
	<property name="system.charset" value="utf-8" />
	<!-- 日志格式 -->
	<property name="system.pattern" value="[${system.name}] %d{YYYY-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} %file:%line - %m%n" />

	<appender name="file" class="ch.qos.logback.core.rolling.RollingFileAppender">
		<file>${system.path}/${system.name}.log</file>
		<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
			<maxHistory>${system.history}</maxHistory>
			<fileNamePattern>${system.path}/%d{yyyy-MM, aux}/${system.name}.%d{yyyy-MM-dd}.log.gz</fileNamePattern>
		</rollingPolicy>
		<bufferSize>${system.buffer}</bufferSize>
		<immediateFlush>false</immediateFlush>
		<encoder>
			<charset>${system.charset}</charset>
			<pattern>${system.pattern}</pattern>
		</encoder>
	</appender>
	<appender name="fileAsync" class="ch.qos.logback.classic.AsyncAppender">
		<appender-ref ref="file" />
		<queueSize>${system.queue}</queueSize>
		<includeCallerData>true</includeCallerData>
		<discardingThreshold>0</discardingThreshold>
	</appender>

	<appender name="console" class="ch.qos.logback.core.ConsoleAppender">
		<encoder>
			<charset>${system.charset}</charset>
			<pattern>${system.pattern}</pattern>
		</encoder>
	</appender>
	
	<springProfile name="dev">
		<root level="DEBUG">
			<appender-ref ref="file" />
			<appender-ref ref="console" />
		</root>
	</springProfile>
	
	<springProfile name="release">
		<root level="INFO">
			<appender-ref ref="file" />
		</root>
	</springProfile>

</configuration>