log4j reset properties back to original as in log4j.properties file

℡╲_俬逩灬. 提交于 2019-12-08 01:20:48

问题


In my application, I defined log4j.properites as follows

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


回答1:


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. 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




回答2:


This is a partial answer (I know); But you can do a reset without explicit LogManager.resetConfiguration();

Resetting Hierarchy

The hierarchy will be reset before configuration when log4j.reset=true is present in the properties file.

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



来源:https://stackoverflow.com/questions/7880828/log4j-reset-properties-back-to-original-as-in-log4j-properties-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!