spring boot1.5.x版本的日志配置一直有一个问题,就是不能直接通过yml配置文件进行日志文件大小进行动态和方便的配置。
怎么解决?直接在springboot项目的maven工程中的src/main/resources的目录下加入如下logback-spring.xml文件,logback-spring命名的logback配置文件在spring boot中加载的优先级是最高的,所以项目一起动就可以加载生效。
其中我在配置文件中添加了控制大小的变量。具体配置文件内容如下:
<?xml version="1.0" encoding="UTF-8"?> <configuration debug="false" scan="true" scanPeriod="60 seconds"> <include resource="org/springframework/boot/logging/logback/defaults.xml"/> <include resource="org/springframework/boot/logging/logback/console-appender.xml"/> <contextName>logback</contextName> <springProperty scope="logback" name="LOG_LEVEL" source="logging.logLevel" defaultValue="info" /> <springProperty scope="logback" name="APP_NAME" source="spring.application.name" defaultValue="" /> <springProperty scope="logback" name="PATTERN_FILE" source="logging.pattern.file" defaultValue="%d{yyyy-MM-dd HH:mm:ss.SSS} ${LOG_LEVEL_PATTERN:-%5p} ${PID:- } --- [%t] %-40.40logger{39} : %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}" /> <springProperty scope="logback" name="MAX_FILESIZE" source="logging.maxfilesize" defaultValue="100MB" /> <property name="LOG_FILE" value="${LOG_FILE:-${LOG_PATH:-${LOG_TEMP:-${java.io.tmpdir:-/tmp}}}/${APP_NAME}.log}"/> <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender"> <file>${LOG_FILE}</file> <layout class="ch.qos.logback.classic.PatternLayout"> <pattern> ${PATTERN_FILE} </pattern> </layout> <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy"> <fileNamePattern>${LOG_FILE}.%i</fileNamePattern> <!-- <maxHistory>${maxhistory}</maxHistory> --> </rollingPolicy> <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy"> <maxFileSize>${MAX_FILESIZE}</maxFileSize> </triggeringPolicy> </appender> <root level="${LOG_LEVEL}"> <appender-ref ref="FILE" /> <appender-ref ref="CONSOLE" /> </root> </configuration>
同时还在yml文件中加入了对应的配置标签
logging: file: ../logs/aaaaa.log maxfilesize: 100MB logLevel: info
maxfilesize为自定义标签,进行设置每个日志文件的大小