I'm trying to reduce duplication in my log4j configuration and wanted to know if I could push similar config down to a root.xml file and inherit from it in each of the child log4j.xml files?
Thank you!
I'm trying to reduce duplication in my log4j configuration and wanted to know if I could push similar config down to a root.xml file and inherit from it in each of the child log4j.xml files?
Thank you!
I doubt that it is possible in log4j but the self named successor project logback at least provides a possibility to include configurations. It is not inheritence but also a strategy to reduce duplications. Maybe you can give it a try.
AFAIK there's no "native" inheritance mechanism, but you may achieve the same result using an entity to reference and include an external xml fragment (see this nabble thread). If you just want to modify certain properties, a similar solution is described here. An Example using external entities:
Main Config (log4j.xml):
appender.xml:
root.xml:
It even works if both the root and the appender definition are placed into a single file, without the ?xml pi. So, the external content may even be an ill-formed xml fragment lacking a single root element. This allows for transferring the complete log4j config into an single external file:
log4j.xml:
log4j-config.txt:
If you need to adapt the log4j config in an individual and flexible way, you could try and merge the root.xml with the child.xml using XSLT or XmlMerge (part of el4j) to create a config on the fly, and feed the org.apache.log4j.xml.DOMConfigurator with the resulting DOM.