<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="true" scanPeriod="60" debug="false">
	<!-- 日志最大的历史 30天 -->
	<springProperty name="MAX_HISTORY" scope="context" source="logging.file.max-history" defaultValue="30"/>
	<!--    获取应用程序名称-->
	<springProperty name="APP_NAME" scope="context" source="spring.application.name"/>
	<!--定义日志文件的存储地址 -->
	<springProperty name="LOG_HOME" scope="context" source="logging.file.path" defaultValue="./logs" />
	<!--    日志文件大小-->
	<springProperty name="LOG_FILESIZE" scope="context" source="logging.file.max-size" defaultValue="30MB"/>

	<property name="COLOR_PATTERN" value="%d{yyyy-MM-dd HH:mm:ss.SSS} ${PID} [%thread] %highlight(%-5level) [%file:%line] | %cyan(%logger{50}) - %msg%n}" />
	<!--    控制台日志模式无颜色-->
	<property name="CONSOLE_LOG_PATTERN_NO_COLOR" value="%d{yyyy-MM-dd HH:mm:ss.SSS} ${PID} [%thread] %(%-5level) [%file:%line] | %(%logger{50}) - %msg%n}" />
	<!-- 控制台输出 -->
	<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
		<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
			<level>debug</level>
		</filter>
		<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
			<!--格式化输出：%d表示日期，%thread表示线程名，%-5level：级别从左显示5个字符宽度%msg：日志消息，%n是换行符-->
			<pattern>${COLOR_PATTERN}</pattern>
			<!-- 设置字符集 -->
			<charset>UTF-8</charset>
		</encoder>
	</appender>

	<!-- 按照每天生成日志文件 -->
	<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
		<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
			<!--设置日志级别,过滤掉info日志,只输入error日志-->
			<level>INFO</level>
		</filter>
		<!-- 文件路径 -->
		<file>${LOG_HOME}/${APP_NAME}.log</file>
		<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
			<!--日志文件输出的文件名 -->
			<FileNamePattern>${LOG_HOME}/${APP_NAME}-%d{yyyy-MM-dd}.%i.log</FileNamePattern>
			<!--日志文件保留天数 cleanHistoryOnStart:清理日志-->
			<cleanHistoryOnStart>true</cleanHistoryOnStart>
			<MaxHistory>${MAX_HISTORY}</MaxHistory>
			<maxFileSize>${LOG_FILESIZE}</maxFileSize>
		</rollingPolicy>
		<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
			<!--格式化输出：%d表示日期，%thread表示线程名，%-5level：级别从左显示5个字符宽度%msg：日志消息，%n是换行符 -->
			<pattern>${CONSOLE_LOG_PATTERN_NO_COLOR}</pattern>
		</encoder>
	</appender>


	<!--	异步记录日志-->
	<appender name="INFO_LOG_ASYNC" class="ch.qos.logback.classic.AsyncAppender">
		<discardingThreshold>0</discardingThreshold>
		<appender-ref ref="FILE"/>
	</appender>


	<springProfile name="dev,test">
		<!--日志等级设置 -->
		<logger name="com.apache.ibatis" level="DEBUG" />
		<logger name="java.sql.Connection" level="DEBUG" />
		<logger name="java.sql.Statement" level="DEBUG" />
		<logger name="java.sql.PreparedStatement" level="DEBUG" />
		<root level="INFO">
			<appender-ref ref="STDOUT" />
			<appender-ref ref="INFO_LOG_ASYNC"/>
		</root>
	</springProfile>
	<springProfile name="prod">
		<root level="INFO">
			<appender-ref ref="STDOUT" />
			<appender-ref ref="INFO_LOG_ASYNC"/>
		</root>
	</springProfile>
</configuration>
