当前位置:网站首页>Log4j dynamic loading configuration file
Log4j dynamic loading configuration file
2022-07-29 01:21:00 【MrMoving】
Application scenarios and problems
When the project is running , If we need to modify log4j 1.X perhaps log4j2 Configuration file for , Generally speaking, we cannot directly stop the project and then modify the file for redeployment . So there is such a problem : How to do this without stopping the current project , Let the system automatically monitor the modification status of the configuration file , So as to realize the function of dynamically loading configuration files ? and log4j 1.X and log4j2 The difference is slightly larger , How should they realize this function ?
log4j 1.X Load configuration files dynamically
log4j 1.X Provides a method to dynamically load configuration files :
DOMConfigurator.configureAndWatch()
PropertyConfigurator.onfigureAndWatch()
DOMConfigurator The corresponding is xml The configuration file ,PropertyConfigurator The corresponding is properties The configuration file . Both classes have configureAndWatch This method , This method has an overloaded method , as follows :
configureAndWatch(String configFilename)
configureAndWatch(String configFilename, long delay)
configureAndWatch Method is used to monitor whether the configuration file has been changed , The monitoring interval is delay Parameters to determine , If this parameter is not passed in, the default time interval is used 1 minute (60000L).configureAndWatch(String configFilename) It's actually called configureAndWatch(String configFilename, long delay).
Code example :
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(" Turn on log4j Error in log monitoring ", e);
}
log4j2 Load configuration files dynamically
and log4j 1.X Compare to ,log4j2 The dynamic loading configuration of is very simple , There is no need to call api, The method is as follows :
<configuration monitorInterval="30">
...
</configuration>
stay log4j2.xml In the configuration file configuration Node add monitorInterval Value , The unit is seconds , If the configured value is greater than 0, It will automatically scan whether the configuration file has been modified according to the time interval , And reload the latest configuration file after modification . If this value is not configured , The default is 0, That is, do not scan whether the configuration file has been modified .
Reference article
The underlying implementation principle of dynamically loading configuration files watchDog
https://www.cnblogs.com/yulinlewis/p/10177196.html
边栏推荐
- 【刷题笔记】二进制链表转整数
- (完美解决)为什么在train/val/test数据集上用train模式效果都很好,但是在eval模式下全部很差
- Letax record \documentclass{}, authoryear attribute is used
- Mathematical modeling and detailed explanation of basic knowledge (common knowledge points of Chemistry)
- Oozie job scheduling
- 一文搞懂ES6基本全部语法
- Django使用MySQL数据库已经存在的数据表方法
- 教你一文解决 js 数字精度丢失问题
- A new generation of ultra safe cellular battery, Sihao aipao, is on the market, starting from 139900
- Thread lock and its ascending and descending levels
猜你喜欢
![“index [hotel/jXLK5MTYTU-jO9WzJNob4w] already exists“](/img/f2/37a1e65eb1104d72128f96fc5d9c85.png)
“index [hotel/jXLK5MTYTU-jO9WzJNob4w] already exists“

新一代超安全蜂窝电池,思皓爱跑上市,13.99万起售

【Leetcode-滑动窗口问题】

Connect with Alipay payment

State compression DP Mondrian's dream

Wechat campus bathroom reservation applet graduation design finished product (8) graduation design thesis template

可视化全链路日志追踪

Beginner's Guide to electronic bidding

Wechat campus bathroom reservation of small program completion work (6) opening defense ppt

18 diagrams, intuitive understanding of neural networks, manifolds and topologies
随机推荐
ActiveMQ基本详解
Cookie和Session
测试/开发程序员靠技术渡过中年危机?提升自己本身的价值......
How to smoothly go online after MySQL table splitting?
A new generation of ultra safe cellular battery, Sihao aipao, is on the market, starting from 139900
Google play APK uploads other international app stores
线程锁及锁的升降级
Brushless DC motor controller (how much is the hall charge for changing the motor)
【ManageEngine】助力哈尔滨工程大学实现网络流量一体化监控管理
20220728 sorting strings that are not pure numbers
【刷题笔记】链表内指定区间反转
ActiveMQ basic details
SDRAM Controller Design (two design methods of digital controller)
Oozie工作调度
Principle and usage setting of large page memory
Wechat campus bathroom reservation of small program completion work (6) opening defense ppt
mysql分表之后怎么平滑上线?
Error reporting: SQL syntax error in flask. Fields in SQL statements need quotation marks when formatting
Cookies and sessions
(完美解决)为什么在train/val/test数据集上用train模式效果都很好,但是在eval模式下全部很差