views:

216

answers:

3

I'm writing a custom log4j appender, and I want to rely on another configured appender as a fallback, in case my (Database) appender fails.

How can I guarantee order of construction of the appenders? My appender's activateOptions() method tries to access another appender and fails because it's not constructed/registered yet.

A: 

If it turns out that there isn't a way of doing what you want, you could create and configure the "fallback" appender from inside your "primary" appender, rather than leaving it to log4j to configure. Not a very nice solution, but I'm not aware of a way of adding dependencies between appenders otherwise.

skaffman
+1  A: 

I suggest to move/copy the config options for the second appender into the config of your custom appender and then create the second appender yourself inside of your custom appender.

Aaron Digulla
Wow, your answer seems like it's directed at another question.Yes, I know appenders are not mysterious.Yes, I know the config is mapped to setters/getters (and I miss C#'s properties, BTW)What I _asked_ about is dependencies between different loggers. How is your answer relevant?
ripper234
Strike that - dependencies between different _appenders_, not _loggers_.
ripper234
Fixed. I guess that was a bit hard to read out of my text.
Aaron Digulla
Yeah, that's what I ended up doing. Less clean, but it'll work.
ripper234
+1  A: 

If you are using a configration file in XML, then you can take advantage of the fact that the order of declaration of appenders in an XML file matters. The appender which is declared first will be configured first. If you are using a configuration file in .properties format, then their order of configuration depends on the order in which they are referenced by loggers a.k.a. categories. The appender which is references first will be configured first.

You could also have a look at logback, log4j's successor which is quite well documented.

Ceki