简体   繁体   English

log4j将属性重置为原始属性,如log4j.properties文件中所示

[英]log4j reset properties back to original as in log4j.properties file

In my application, I defined log4j.properites as follows 在我的应用程序中,我将log4j.properites定义如下

log4j.appender.email=org.apache.log4j.net.SMTPAppender
log4j.appender.email.Subject=email Notification

later in the program i am dynamically changing the subject to 在程序的后面,我正在动态地将主题更改为

Properties prop = new Properties();
prop.setProperty("log4j.appender.email.Subject", "Test Completed");

After I use this variable, I wan to reset this back to original on file. 使用此变量后,我希望将其重置为原始文件。 so I did this 所以我做到了

LogManager.resetConfiguration();
PropertyConfigurator.configure(prop);

But, later in the code whenever I use this subject property it is giving its value as 'Test Completed'. 但是,稍后在代码中,每当我使用此主题属性时,它都会将其值指定为“测试完成”。 Any suggestion to reset configuration is greatly appreciated. 任何重置配置的建议都将不胜感激。 Thanks 谢谢

As stated in my comments above both 正如我在上面的评论中所述

LogManager.resetConfiguration();
PropertyConfigurator.configure(prop);

don't reconfigure already existing Logger instances, so that they still use your old EmailAppender. 不要重新配置现有的Logger实例,以使它们仍使用您的旧EmailAppender。 In order to make changes to take effect you should recreate loggers. 为了使更改生效,您应该重新创建记录器。 If it's not possible (your loggers are static final fields for example), you can create a simple Logger wrapper, which will register itself with some listener, that will notify on configuration change, so that wrapper can create fresh logger instance 如果不可能(例如,您的记录器是静态的final字段),则可以创建一个简单的Logger包装器,该包装器将向一些侦听器进行注册,并通知配置更改,以便包装器可以创建新的记录器实例

This is a partial answer (I know); 这是部分答案(我知道); But you can do a reset without explicit LogManager.resetConfiguration(); 但是您可以在没有显式LogManager.resetConfiguration();情况下进行重置LogManager.resetConfiguration();

Resetting Hierarchy 重置层次结构

The hierarchy will be reset before configuration when log4j.reset=true is present in the properties file. 当属性文件中存在log4j.reset = true时,将在配置之前重置层次结构。

https://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/PropertyConfigurator.html https://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/PropertyConfigurator.html

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

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