问题:
SpringBoot + LogBack 写日志,除了正常的日志之外,程序跑起来后会在项目根目录多生成一个 LOG_PATH_IS_UNDEFINED 的文件夹,我的配置是以下:
applickation.yml
#log logging: config: classpath:logback-spring.xml path: d:/log/outerlink #log end
logback-spring.xml
<?xml version="1.0" encoding="UTF-8"?> <configuration> <include resource="org/springframework/boot/logging/logback/base.xml"/> <appender name="ERROR_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender"> <filter class="ch.qos.logback.classic.filter.ThresholdFilter"> <level>ERROR</level> </filter> <File>${LOG_PATH}/error.log</File> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <fileNamePattern>${LOG_PATH}/error-%d{yyyyMMdd}.log.%i</fileNamePattern> <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP"> <maxFileSize>50MB</maxFileSize> </timeBasedFileNamingAndTriggeringPolicy> <maxHistory>2</maxHistory> </rollingPolicy> <layout class="ch.qos.logback.classic.PatternLayout"> <Pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} -%msg%n</Pattern> </layout> </appender> <root level="INFO"> <appender-ref ref="ERROR_FILE" /> </root> </configuration>
解决办法 :
新建 bootstrap.yml,把日志配置写到里面
原因:
Spring容器在Logback初始化之后设置LOG_PATH ,所以在 logback-spring.xml 找不到 LOG_PATH。但 bootstrap.yml 由父Spring ApplicationContext加载,父ApplicationContext被加载到使用application.yml的之前
注:
发现只有在 pom.xml 加入spring cloud相关包才能加载 bootstrap.yml , 具体原因还不是很明白 。我是加入了
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-feign</artifactId> <version>1.4.2.RELEASE</version> </dependency>