logback-spring.xml
<?xml version="1.0" encoding="UTF-8"?>
<!--scan自动扫描重新加载配置 scanPeriod重新加载配置的周期-->
<configuration debug="true" scan="true" scanPeriod="10 seconds">
<!--凡是springProperty的配置都将出现在生成的json中,如果springProperty 原json中已有相同key则将覆盖-->
<springProperty scope="context" name="projectName" source="spring.application.name"/>
<springProperty scope="context" name="server.port" source="server.port"/>
<springProperty scope="context" name="log.level" source="log.level"/>
<springProperty scope="context" name="log.path" source="log.path"/>
<contextName>${projectName}</contextName>
<!--ConsoleAppender 用于在屏幕上输出日志-->
<appender name="stdout" class="ch.qos.logback.core.ConsoleAppender">
<!--定义了一个过滤器,在LEVEL之下的日志输出不会被打印出来-->
<!--这里定义了 DEBUG,也就是控制台不会输出比ERROR级别小的日志-->
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>INFO</level>
</filter>
<!-- encoder 默认配置为PatternLayoutEncoder -->
<!--定义控制台输出格式-->
<encoder>
<!--<pattern>%d [%thread] %-5level %logger{36} [%file : %line] - %msg%n</pattern>-->
<pattern>%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<appender name="file" class="ch.qos.logback.core.rolling.RollingFileAppender">
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>INFO</level>
</filter>
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<!-- 基于时间和日志文件大小的轮转策略 -->
<fileNamePattern>${log.path}/${projectName}.%d{yyyy-MM-dd}.%i.log</fileNamePattern>
<maxFileSize>100MB</maxFileSize><!--单个文件最大为100MB-->
<maxHistory>5</maxHistory><!--保存5天的历史记录-->
<totalSizeCap>3GB</totalSizeCap><!--归档文件总大小3G-->
</rollingPolicy>
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<springProfile name="local-chenyan">
<root level="info">
<appender-ref ref="stdout"/>
<appender-ref ref="file"/>
</root>
</springProfile>
<springProfile name="dev">
<root level="info">
<appender-ref ref="stdout"/>
<appender-ref ref="file"/>
</root>
</springProfile>
<springProfile name="test">
<root level="info">
<appender-ref ref="stdout"/>
<appender-ref ref="file"/>
</root>
</springProfile>
<springProfile name="prod">
<root level="info">
<appender-ref ref="stdout"/>
<appender-ref ref="file"/>
</root>
</springProfile>
</configuration>
网友评论