views:

29

answers:

1

I use log4j in my project and wanted to use FallbackErrorHandler for Backup-Reasons. So, when I want to implement a FallbackErrorHandler, you have to use the DOMConfigurator and that for a XML-Configurationfile.

But now, my customer doesn´t really like XML and looking for a way, he can configure the Logging-behaviour himself, I got the idea to load the basic configuration with the help of java-properties und instantiating the higher Appender or ErrorHandler with Java-Code, which I could configure with parameters.

The configuration of the ErrorHandler worked so far, the log4j-debug showing everythings correct, but I ran into a problem I could not solve until yet. My FallbackErrorHandler starts logging to the backupAppender already at the start of the programm, not when a standard appender failed. I couldn´t figure out what I´ve done wrong, I can post the code of the properties and the java-code if somebody wants to look at it, but at first, I wanted to ask, whether somebody ran into the same problem or has any experiences with configuring log4j within Java?

+1  A: 

After weeks of trying I now finally found a solution. The key event was the discovery, that also the PropertyConfigurator has a class for ErrorHandler, although Geki wrote, that there is no possibility to configure a ErrorHandler with the PropertyConfigurator. So I went on trying.

Now I could simply combine the basic declaration in the log4j.properties with the linking of Logger, Appender and ErrorHandler in JavaCode. So following stands in my Properties:

log4j.appender.ServerAppender=org.apache.log4j.FileAppender
log4j.appender.ServerAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.ServerAppender.layout.ConversionPattern=%d{dd MMM yyyy HH:mm:ss,SSS} [%t] %-5p %F - %m%n 
log4j.appender.ServerAppender.file=logs/ServerLog.txt
log4j.appender.ServerAppender.threshold=DEBUG

log4j.logger.com.foo.server=TRACE, ServerAppender

log4j.appender.ServerAppender.errorhandler=org.apache.log4j.varia.FallbackErrorHandler
log4j.appender.ServerAppender.errorhandler.logger-ref=com.foo.server
log4j.appender.ServerAppender.errorhandler.appender-ref=FallbackServerAppender

log4j.appender.FallbackServerAppender=org.apache.log4j.FileAppender
log4j.appender.FallbackServerAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.FallbackServerAppender.layout.ConversionPattern=%d{dd MMM yyyy HH:mm:ss,SSS} [%t] %-5p %F - %m%n 
log4j.appender.FallbackServerAppender.file=C:/Temp/Test/fallbackServerLog.txt

log4j.logger.com.foo.error=DEBUG,FallbackServerAppender

While com.foo.server is a part of my package hierarchy, com.foo.error is only a placeholder, so I can call it within Java-Code. That will look like this.

PropertyConfigurator.configure(LOG4JCONFIG);
FallbackErrorHandler fbeh=(FallbackErrorHandler)LogManager.getLogger("com.foo.server").getAppender("ServerAppender").getErrorHandler();
fbeh.setLogger(LogManager.getLogger("com.foo.server"));
fbeh.setAppender(LogManager.getLogger("com.foo.server").getAppender("ServerAppender"));         
fbeh.setBackupAppender(LogManager.getLogger("com.foo.error").getAppender("FallbackServerAppender"));

A known problem, the fallbackErrorHandler doesn´t reset when the Appender is on again, could be solved by restarting the configure()-method in or after situations, where normally the normal Appender can´t log.

Hope it could help somebody :)