Spring Boot项目的Logback配置文件使用yaml格式
1、普通的Spring项目使用logback默认用properties文件做为配置变量。
2、如果非要用yaml文件,那么可以转成Spring Boot项目,天生无缝结合
3、没办法,如果项目配置文件用了yaml,那么你的logback只能自己另开properties文件进行单独配置
以下是基于Spring Boot的yaml配置文件示例:
application.yml:
# 日志配置 为空为项目跟目录下的logs 或者指定已经存在的目录 log: path: /data/weblog/business/www.test.com
logback-spring.xml:
<?xml version="1.0" encoding="UTF-8"?> <!-- 说明: 1. 文件的命名和加载顺序有关 logback.xml早于application.yml加载,logback-spring.xml晚于application.yml加载 如果logback配置需要使用application.yml中的属性,需要命名为logback-spring.xml 2. logback使用application.yml中的属性 使用springProperty才可使用application.yml中的值 可以设置默认值 --> <configuration scan="true" scanPeriod="60 seconds"> <!-- log base path --> <springProperty scope="context" name="logPath" source="log.path" defaultValue="logs"/> <!-- log name --> <property name="LOG_HOME" value="${logPath}"/> <!-- back log base path --> <property name="LOG_BACK_HOME" value="${logPath}/backup"/> <property name="SRVNAME" value="clsapi-console"/> <!-- 文件切割大小 --> <property name="maxFileSize" value="100MB" /> <!-- 文档保留天数 --> <property name="maxHistory" value="60" /> <!-- 文档保留总大小 --> <property name="totalSizeCap" value="10GB" /> <!-- 系统服务日志 --> <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender"> <file>${LOG_HOME}/${SRVNAME}.log</file> <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy"> <!-- daily rollover --> <fileNamePattern>${LOG_BACK_HOME}/%d{yyyy-MM-dd}/${SRVNAME}.%d{HH}.%i.log.gz</fileNamePattern> <!-- 单个日志文件最多 100MB, 60天的日志周期,最大不能超过10GB --> <maxFileSize>${maxFileSize}</maxFileSize> <maxHistory>${maxHistory}</maxHistory> <totalSizeCap>${totalSizeCap}</totalSizeCap> </rollingPolicy> <encoder> <pattern>%d{yyyyMMdd HH:mm:ss.SSS} %X{LOG_ID} [%thread] %-5level %logger{100}.%method\(\):%L - %msg%n</pattern> </encoder> </appender> <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> <!-- On Windows machines setting withJansi to true enables ANSI color code interpretation by the Jansi library. This requires org.fusesource.jansi:jansi:1.8 on the class path. Note that Unix-based operating systems such as Linux and Mac OS X support ANSI color codes by default. recognizes "%black", "%red", "%green","%yellow","%blue", "%magenta","%cyan", "%white", "%gray", "%boldRed","%boldGreen", "%boldYellow", "%boldBlue", "%boldMagenta""%boldCyan", "%boldWhite" and "%highlight" --> <!--withJansi>true</withJansi--> <encoder> <!--%d{yyyy-MM-dd HH:mm:ss.SSS} -%5p ${PID:-} [%15.15t] %-40.40logger{39} : %m%n--> <pattern>%boldCyan(%d{yyyy-MM-dd HH:mm:ss.SSS}) - %boldRed(%5p) %blue([%10.10t]) %magenta(%-35.35logger{20}) %yellow(%2M) %green(%2L) : %msg%n</pattern> <!--<pattern>%d{yyyyMMddHHmmss} [%thread] [%c %2M %2L] %-3p - %m%n</pattern>--> </encoder> </appender> <root level="info"> <appender-ref ref="STDOUT"/> <appender-ref ref="FILE"/> </root> <logger name="com.easonjim.clsapi.aspect.LogsAspect" level="debug"> <appender-ref ref="api_call_file"/> </logger> <logger name="org.springframework.web.servlet" level="info"/> </configuration>
4、如果你非要用yaml文件,那么只能使用绝对路径,例如:
logback-spring.xml引入文件:
<property file="${HOME}/configuration/application.yaml"/>
application.yaml:
LOG_FILE : C:/logs
logback-spring.xml使用:
<appender name="MY_APPENDER" class="ch.qos.logback.core.rolling.RollingFileAppender"> <file>${LOG_FILE}</file> ... </appender>
logback-spring.xml增加扫描时间:
<configuration scan="true" scanPeriod="10 seconds">
参考:
https://stackoverflow.com/questions/42648195/how-to-set-logback-xml-properties-in-application-yaml