当前位置:网站首页>log4j动态加载配置文件
log4j动态加载配置文件
2022-07-29 00:09:00 【MrMoving】
应用场景与问题
当项目在运行时,我们如果需要修改log4j 1.X或者log4j2的配置文件,一般来说我们是不能直接将项目停止运行再来修改文件重新部署的。于是就有这样一个问题:如何在不停止当前项目的运行的情况下,让系统能够自动地监控配置文件的修改状况,从而实现动态加载配置文件的功能?而log4j 1.X和log4j2的差别略大,各自应该怎么实现这个功能?
log4j 1.X动态加载配置文件
log4j 1.X提供了动态加载配置文件的方法:
DOMConfigurator.configureAndWatch()
PropertyConfigurator.onfigureAndWatch()
DOMConfigurator对应的是xml配置文件,PropertyConfigurator对应的是properties配置文件。这两个类都有configureAndWatch这个方法,该方法有个重载方法,如下:
configureAndWatch(String configFilename)
configureAndWatch(String configFilename, long delay)
configureAndWatch方法用来监控配置文件是否被改动,监控的时间间隔是delay参数来决定,如果不传入该参数则使用默认的时间间隔1分钟(60000L)。configureAndWatch(String configFilename)实际上还是调用的configureAndWatch(String configFilename, long delay)。
代码示例:
import org.springframework.util.ResourceUtils;
import org.springframework.util.SystemPropertyUtils;
import org.apache.log4j.PropertyConfigurator;
try {
String resolvedLocation = SystemPropertyUtils.resolvePlaceholders("classpath:log4j.properties");
File file = ResourceUtils.getFile(resolvedLocation);
if (file.exists()) {
PropertyConfigurator.configureAndWatch(file.getAbsolutePath(), TimeUnit.SECONDS.toMillis(5));
} else {
LOGGER.error("classpath:log4j.properties");
}
} catch (Exception e) {
LOGGER.error("开启log4j日志监控报错了", e);
}
log4j2动态加载配置文件
和log4j 1.X比起来,log4j2的动态加载配置很简单就能实现,不需要另外在代码中调用api,方法如下:
<configuration monitorInterval="30">
...
</configuration>
在log4j2.xml配置文件中的configuration节点添加monitorInterval的值,单位是秒,如果配置的值大于0,则会按照时间间隔来自动扫描配置文件是否被修改,并在修改后重新加载最新的配置文件。如果不配置该值,默认为0,即不扫描配置文件是否被修改。
参考文章
动态加载配置文件的底层实现原理watchDog
https://www.cnblogs.com/yulinlewis/p/10177196.html
边栏推荐
- [notes for question brushing] delete continuous nodes with a total value of zero from the linked list
- Synchronized关键字详解
- [target detection] Introduction to yolor theory + practical test visdrone data set
- 线程锁及锁的升降级
- ThinkPHP high imitation blue cloud disk system program
- DDD领域驱动设计如何进行工程化落地
- 进程和线程知识点总结1
- 面试官:程序员,请你告诉我是谁把公司面试题泄露给你的?
- ACM SIGIR 2022 | 美团技术团队精选论文解读
- Time series prediction | MATLAB realizes time series prediction of TCN time convolution neural network
猜你喜欢

递归与分治

如何在WordPress中创建一个自定义404错误页面

数字孪生轨道交通:“智慧化”监控疏通城市运行痛点

时间复杂度、空间复杂度的学习总结

ACM SIGIR 2022 | interpretation of selected papers of meituan technical team

Tupu software appeared at the 2022 Fuzhou digital Expo to jointly create a new digital era

Summary of process and thread knowledge points 1

Connect with Alipay payment

Necessary interview skills for Android (including interview questions and learning materials)

Wechat campus bathroom reservation applet graduation design finished product (8) graduation design thesis template
随机推荐
Naver 三方登录
nep 2022 cat
Date conversion EEE MMM DD hh:mm:ss zzz YYYY
RHCE命令练习(一)
电子招标初学者指南
Django使用MySQL数据库已经存在的数据表方法
🧐 Table1 | finish your third line watch in one second
如何在WordPress中创建一个自定义404错误页面
Visual full link log tracking
【mysql】字符串转int
Oozie工作调度
mysql分表之后怎么平滑上线?
[unity] configure unity edit C as vscode
How to deal with the time, scope and cost constraints in the project?
C language bracket matching (stack bracket matching C language)
Recursion and divide and conquer
dart数组,Map,类型判断,条件判断运算符,类型转换
system verilog常用语法
【ManageEngine】助力哈尔滨工程大学实现网络流量一体化监控管理
RHCE命令练习(二)