Java Code :
DOMConfigurator.configureAndWatch(log4jConfigFile.getAbsolutePath(),100000L);
I need to add some appenders programmaticaly whenever Thread created by configureAndWatch method reloads the log4j.xml.
Actually i am setting some appenders programmatically but its get reset when Thread reloads the log4j.xml so i need to again add the appenders programmatically.For this i need a callback when ever Thread reloads the log4j.xml
Any Idea ?? How do i do this….
Unfortunately Log4j does not provide a way to register a callback on configuration load. It just creates a “watchdog” thread to monitor the file and reload configuration on file changes.
The only way I see it done is by spawning your own thread that reloads configuration and also adds the extra appenders.
Instead of :
you do this:
You configured Log4j, added the appenders and started the monitoring thread. In the thread you reload configuration on file change:
Have a look at the source code of these classes for further details: DOMConfigurator (scroll to the bottom) and FileWatchdog.
My suggestion though will be to just have everything configured in the
log4j.xmlfile and not resort to workarounds.P.S. You don’t mention what type of Java application you have, but be aware that
configureAndWatchcomes with a warning in Java EE applications as the thread does not stop on application shutdowns; it stops only on JVM shutdown.