springboot项目添加了logback-spring.xml配置文件不生效

springboot,logback-spring.xml不生效

通常情况下springboot项目,
只需在src/main/resources下添加logback-spring.xml即可,

logback-spring.xml

<?xml version="1.0" encoding="UTF-8"?>
<configuration debug="false" scan="false">
	<property name="log.path" value="/opt/pig/logs/${project.artifactId}"/>
	<!-- 彩色日志格式 -->
	<property name="CONSOLE_LOG_PATTERN"
			  value="${CONSOLE_LOG_PATTERN:-%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} %clr(${LOG_LEVEL_PATTERN:-%5p}) %clr(${PID:- }){magenta} %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}}"/>
	<!-- 彩色日志依赖的渲染类 -->
	<conversionRule conversionWord="clr" converterClass="org.springframework.boot.logging.logback.ColorConverter"/>
	<conversionRule conversionWord="wex"
					converterClass="org.springframework.boot.logging.logback.WhitespaceThrowableProxyConverter"/>
	<conversionRule conversionWord="wEx"
					converterClass="org.springframework.boot.logging.logback.ExtendedWhitespaceThrowableProxyConverter"/>
					
	<!-- Console log output -->
	<appender name="console" class="ch.qos.logback.core.ConsoleAppender">
		<encoder>
			<pattern>${CONSOLE_LOG_PATTERN}</pattern>
		</encoder>
	</appender>

	<!-- Log file debug output -->
	<appender name="debug" class="ch.qos.logback.core.rolling.RollingFileAppender">
		<file>${log.path}/debug.log</file>
		<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
			<fileNamePattern>${log.path}/%d{yyyy-MM, aux}/debug.%d{yyyy-MM-dd}.%i.log.gz</fileNamePattern>
			<maxFileSize>50MB</maxFileSize>
			<maxHistory>30</maxHistory>
		</rollingPolicy>
		<encoder>
			<pattern>%date [%thread] %-5level [%logger{50}] %file:%line - %msg%n</pattern>
		</encoder>
	</appender>

	<!-- Log file error output -->
	<appender name="error" class="ch.qos.logback.core.rolling.RollingFileAppender">
		<file>${log.path}/error.log</file>
		<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
			<fileNamePattern>${log.path}/%d{yyyy-MM}/error.%d{yyyy-MM-dd}.%i.log.gz</fileNamePattern>
			<maxFileSize>50MB</maxFileSize>
			<maxHistory>30</maxHistory>
		</rollingPolicy>
		<encoder>
			<pattern>%date [%thread] %-5level [%logger{50}] %file:%line - %msg%n</pattern>
		</encoder>
		<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
			<level>ERROR</level>
		</filter>
	</appender>

	<!-- Level: FATAL 0  ERROR 3  WARN 4  INFO 6  DEBUG 7 -->
	<root level="INFO">
		<appender-ref ref="console"/>
		<appender-ref ref="debug"/>
		<appender-ref ref="error"/>
	</root>
</configuration>

然后我启动项目,却发现并没有日志文件生成到配置的目录,

我又在application.yml中指定了logback的配置文件:

logging:
  config: classpath:logback-spring.xml

可是还是不生效,这又是怎么回事呢?

我的这个项目是开源项目pig的注册中心pig-register,它引用了nacos-core这个jar包,
我感觉是因为这个依赖(因为我在本地启动pig-register的时候在C盘看到过nacos输出的日志文件),我去看了naco-core这个项目,发现它有个META-INF/logback/nacos.xml,
里头是nacos的logback配置文件,
然后又找到在LoggingSpringApplicationRunListener中用到了这个配置文件,
在这里插入图片描述

LoggingSpringApplicationRunListener implements SpringApplicationRunListener

看到这儿,我有点明白是怎么回事了,

spring boot是使用LoggingApplicationListener来进行日志系统的初始化的,

System.setProperty(CONFIG_PROPERTY, DEFAULT_NACOS_LOGBACK_LOCATION);

这一步将LoggingApplicationListener中的CONFIG_PROPERTY即logging.config设置为了"META-INF/logback/nacos.xml",

再来看下nacos-core的/META-INF/spring.factories,
在这里插入图片描述
果然在这里又指定了SpringApplicationRunListener为前面的LoggingSpringApplicationRunListener,
这样pig-register引用nacos-core的时候,会将nacos的LoggingSpringApplicationRunListener注册到spring boot容器中(启动的时候),
这样SpringApplicationRunListener就不会再去resource寻找logback.xml、logback-spring.xml这样的默认配置文件,
所以我们添加的配置文件就不起作用。

那么,是否就没办法修改日志配置了?

不是的,我们可以在启动的时候这样:

nohup java -Dserver.port=8848 -Dlogging.level.root=INFO -Xmx256M -Xms256M -jar pig-register.jar --logging.config=classpath:logback-spring.xml >> /dev/null 2>&1 &

指定使用我们的配置文件。

不当之处,还请指正。

参考文章:

加多SpringBoot之logback-spring.xml不生效的解决方法
cy谭logback.xml 不生效 && spring boot 日志配置文件读取不到
kangkaiispring boot 日志文件配置(logback-spring.xml)亲测可用!
CaptainCatsMETA-INF/spring.factories文件的作用是什么
CaptainCatsnohup java -jar后台启动,参数“> /dev/null 2>&1“的含义。

  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值