2

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

MaDa
  • 10,511
  • 9
  • 46
  • 84
gmeka
  • 4,269
  • 3
  • 25
  • 25
  • 1
    I guess the possible cause of the problem is that you have Logger constructed after you change subject, so it gets Appender with that subject. Later when you reset config you don't recreate you Logger instance which is stuck with old email appender – SirVaulterScoff Oct 25 '11 at 05:04
  • Yeah i've checked the resetConfiguration source and it has nothing to do with appenders, while PropertyConfigurator.configure is just used to read configuration - not reconfigure logger instances. – SirVaulterScoff Oct 25 '11 at 05:06

2 Answers2

5

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

SirVaulterScoff
  • 704
  • 1
  • 6
  • 18
  • @Scoff: I did this But having the same issue that log4j is using the dynamically changed content. I achieved this by reistansiating the properties file every time I change the properties. – gmeka Nov 24 '11 at 03:15
  • May its not suggestible to load the same class many times. Is there any alternate for this – gmeka Nov 24 '11 at 03:16
0

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

ggrandes
  • 2,067
  • 22
  • 16