Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

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.

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top