简体   繁体   English

我可以以编程方式更新 log4j 1.2 xml 配置文件吗?

[英]Can I update log4j 1.2 xml configuration file programmatically?

I know it is possible to reload log4j's xml configuration while the application is running, but the searching I've done only shows how to do this when the XML config file is edited manually via a text editor.我知道可以在应用程序运行时重新加载 log4j 的 xml 配置,但我所做的搜索只显示了当通过文本编辑器手动编辑 XML 配置文件时如何执行此操作。

I want to be able to dynamically change the level of the root logger in my application via a web page and persist that change to the log4j XML configuration, so I won't have to change the log level again if I restart the application.我希望能够通过网页动态更改我的应用程序中的根记录器的级别,并将该更改保存到 log4j XML 配置,因此如果我重新启动应用程序,我将不必再次更改日志级别。 Is it possible to do this, or can the log4j XML file only be updated manually?是否可以这样做,或者只能手动更新 log4j XML 文件?

I am using log4j 1.2 in my application.我在我的应用程序中使用 log4j 1.2。

Many thanks in advance!提前谢谢了!

对于任何发现自己陷入困境的笨蛋,可以使用以下方式将数据写为属性

org.apache.log4j.config.PropertyPrinter;

You can acheive this by PropertyConfigurator configure and watch .您可以通过PropertyConfigurator configure 和 watch来实现这一点。

Read the configuration file configFilename if it exists.读取配置文件 configFilename(如果存在)。 Moreover, a thread will be created that will periodically check if configFilename has been created or modified.此外,将创建一个线程,该线程将定期检查 configFilename 是否已创建或修改。 The period is determined by the delay argument.周期由延迟参数确定。 If a change or file creation is detected, then configFilename is read to configure log4j.如果检测到更改或文件创建,则读取 configFilename 以配置 log4j。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM