当前位置:网站首页>如何指定pig-register项目日志的输出路径
如何指定pig-register项目日志的输出路径
2022-06-23 04:19:00 【CaptainCats】
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 日志配置文件读取不到
kangkaii:spring boot 日志文件配置(logback-spring.xml)亲测可用!
CaptainCats:META-INF/spring.factories文件的作用是什么
CaptainCats:nohup java -jar后台启动,参数“> /dev/null 2>&1“的含义。
边栏推荐
- What does the English letter PC mean? What does the Internet PC mean
- 华为软硬件生态圈成型,从根子上改变美国对软硬件体系的领导地位
- C prime plus notes d'apprentissage - 2, constantes et formatage io (I / o)
- AI艺术的基因工程?使用 #Artbreeder 改变图像的任意形态
- ORB_SLAM2运行
- The performance of nonstandard sprintf code in different platforms
- Win11如何开启移动热点?Win11开启移动热点的方法
- [proteus simulation] Arduino uno+pcf8574+lcd1602+mpx4250 electronic scale
- MySQL character set
- APP SHA1获取程序 百度地图 高德地图获取SHA1值的简单程序
猜你喜欢

数字藏品赋能实体产业释放了哪些利好?

C prime plus notes d'apprentissage - 2, constantes et formatage io (I / o)

ssm项目搭建

What does the English letter PC mean? What does the Internet PC mean

Analysis on the problems and causes of digital transformation of manufacturing industry

MDM data cleaning function development description

【斯坦福计网CS144项目】Lab2: TCPReceiver

MySQL面试真题(二十四)——行列互换

Real MySQL interview question (XXVIII) -- case - Analysis of indicators of communication operators

Activity启动模式和生命周期实测结果
随机推荐
数字藏品如何赋能经济实体?
vant weapp日历组件性能优化 Calendar 日历添加min-date最小日期页面加载缓慢
Wechat applet: a new interesting test
Software design and Development Notes 2: serial port debugging tool based on QT design
iNFTnews | 加密之家从宇宙寄来的明信片,你会收到哪一张?
知识点滴 - 折叠锻打和大马士革钢
True MySQL interview question (21) - Finance - overdue loan
数字化工厂建设可划分为三个方面
Yingjixin ip6806 wireless charging scheme 5W Qi certified peripheral simplified 14 devices
【owt】owt-client-native-p2p-e2e-test vs2017构建 6:修改脚本自动生成vs工程
PAT 乙等 1016 C语言
ssm项目搭建
Differences between fs4059a and fs5080e charging chips
Heimdall Database Proxy横向扩展提高20倍
高等数学(第七版)同济大学 习题1-7 个人解答
51万奖池邀你参战——第二届阿里云ECS CloudBuild开发者大赛来袭
Prometheus, incluxdb2.2 installation and flume_ Export download compile use
opencv显示图像
Lottery DDD code
Digital collections - new investment opportunities