当前位置:网站首页>如何指定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“的含义。
边栏推荐
- Huawei's software and hardware ecosystem has taken shape, fundamentally changing the leading position of the United States in the software and hardware system
- 新课上线 | 每次 5 分钟,轻松玩转阿里云容器服务!
- 数字藏品——新的投资机遇
- Redis cache penetration solution - bloom filter
- 数字藏品火热背后需要强大的技术团队支持 北方技术团队
- 数字藏品到底有什么魔力?目前有哪些靠谱的团队在开发
- @jsonfield annotation in fastjson
- android Handler内存泄露 kotlin内存泄露处理
- [OWT] OWT client native P2P E2E test vs2017 build 6: modify script automatic generation vs Project
- 使用链表实现两个多项式相加和相乘
猜你喜欢

MySQL面试真题(二十八)——案例-通讯运营商指标分析

MySQL面试真题(二十一)——金融-贷款逾期

C primer plus學習筆記 —— 2、常量與格式化IO(輸入/輸出)

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

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

Adnroid activity截屏 保存显示到相册 View显示图片 动画消失

Yingjixin ip6806 wireless charging scheme 5W Qi certified peripheral simplified 14 devices

Prometheus, incluxdb2.2 installation and flume_ Export download compile use

Advanced Mathematics (Seventh Edition) Tongji University exercises 1-8 personal solutions

MySQL面试真题(二十四)——行列互换
随机推荐
Deploy docker and install MySQL in centos7
MDM数据清洗功能开发说明
Behind the hot digital collections, a strong technical team is needed to support the northern technical team
How does win11 enable mobile hotspot? How to enable mobile hotspot in win11
Explanation of penetration test process and methodology (Introduction to web security 04)
MySQL面试真题(二十一)——金融-贷款逾期
MySQL面试真题(二十三)——拼多多-球赛分析
Fs2119a Synchronous Boost IC output 3.3V and fs2119b Synchronous Boost IC output 5V
高等数学(第七版)同济大学 习题1-8 个人解答
Basic calculator for leetcode topic analysis
Oracle exception
MySQL面试真题(二十五)——常见的分组比较场景
Huawei's software and hardware ecosystem has taken shape, fundamentally changing the leading position of the United States in the software and hardware system
51万奖池邀你参战——第二届阿里云ECS CloudBuild开发者大赛来袭
三项最高级认证,两项创新技术、两大优秀案例,阿里云亮相云原生产业大会
数字藏品如何赋能经济实体?
opencv显示图像
PAT 乙等 1022 D进制的A+B
Special research on Intelligent upgrading of heavy trucks in China in 2022
visdom的使用