脚手架搭建之 日志打印

1.yml
logging:
  config: classpath:logback-spring.xml
log:
  level:
    root: info
    my: debug
  file: logs/admin-server.log
  maxsize: 30MB
logback-spring.xml
<?xml version='1.0' encoding='UTF-8'?>
<configuration>
	<springProperty name="logFile" source="log.file" />
	<springProperty name="rootlevel" source="log.level.root" />
	<springProperty name="mylevel" source="log.level.my" />
	<springProperty name="maxFileSize" source="log.maxsize" />

	<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
		<encoder>
			<pattern>%d [%thread] %-5level %logger{50} -[%file:%line]- %msg%n
			</pattern>
			<charset>UTF-8</charset>
		</encoder>
	</appender>

	<appender name="adminLog" class="ch.qos.logback.core.rolling.RollingFileAppender">
		<file>${logFile}</file>
		<encoder>
			<pattern>%d [%thread] %-5level -[%file:%line]- %msg%n
			</pattern>
			<charset>UTF-8</charset>
		</encoder>
		<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
			<fileNamePattern>${logFile}.%d{yyyy-MM-dd}.%i</fileNamePattern>
			<maxFileSize>${maxFileSize}</maxFileSize>
		</rollingPolicy>
	</appender>

	<appender name="errorLog" class="ch.qos.logback.core.rolling.RollingFileAppender">
		<file>${logFile}.error</file>
		<encoder>
			<pattern>%d [%thread] %-5level %logger{36} -[%file:%line]- %msg%n
			</pattern>
			<charset>UTF-8</charset>
		</encoder>
		<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
			<fileNamePattern>${logFile}.error.%d{yyyy-MM-dd}.%i</fileNamePattern>
			<maxFileSize>${maxFileSize}</maxFileSize>
		</rollingPolicy>
		<filter class="ch.qos.logback.classic.filter.LevelFilter">
			<level>error</level>
			<onMatch>ACCEPT</onMatch>
			<onMismatch>DENY</onMismatch>
		</filter>
	</appender>

	<appender name="sqlLog" class="ch.qos.logback.core.rolling.RollingFileAppender">
		<file>${logFile}.sql</file>
		<encoder>
			<pattern>%d [%thread] %msg%n
			</pattern>
			<charset>UTF-8</charset>
		</encoder>
		<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
			<fileNamePattern>${logFile}.sql.%d{yyyy-MM-dd}.%i</fileNamePattern>
			<maxFileSize>${maxFileSize}</maxFileSize>
		</rollingPolicy>
	</appender>

	<logger name="adminLogger" level="${mylevel}" additivity="true">
		<appender-ref ref="adminLog" />
	</logger>

	<logger name="com.zhangmeng.mystyle.dao" level="DEBUG" additivity="true">
		<appender-ref ref="sqlLog" />
	</logger>

	<logger name="org.springframework.scheduling.quartz" level="error" additivity="true">
		<appender-ref ref="adminLog" />
	</logger>

	<root level="${rootlevel}">
		<appender-ref ref="STDOUT" />
		<appender-ref ref="errorLog" />
	</root>

</configuration>

    原文作者:芊芊墨客
    原文地址: https://blog.csdn.net/weixin_45637293/article/details/117330009
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞