Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can log4j inherit xml from a base/root element?

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!

like image 638
jaxonabc Avatar asked May 24 '11 20:05

jaxonabc


People also ask

What is root logger in log4j xml?

the root logger that logs messages with level INFO or above in the all packages to the various destinations: console, e-mail and file, "com. foo" logger that logs messages with level WARN or above in package "com. foo" and its child packages to the another file.

Where should log4j xml be placed?

Locate the log4j. xml file under the oarm/WEB-INF/classes/ directory. Update the log output path for each appender.

What does root level mean log4j?

The level of the root logger is defined as DEBUG, The DEBUG appender named FILE to it. The appender FILE is defined as org. apache. log4j.

Where does log4j2 xml go?

We should put log4j2. xml anywhere in the application's classpath. Log4j will scan all classpath locations to find out this file and then load it. We can find this file mostly placed in the 'src/main/resources' folder.


2 Answers

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):

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd" [
    <!ENTITY appender SYSTEM "appender.xml">
    <!ENTITY root SYSTEM "root.xml">
]>  
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/" >
&appender;
&root;
</log4j:configuration>

appender.xml:

<?xml version="1.0" encoding="UTF-8"?>
<appender name="MyAppender" class="org.apache.log4j.DailyRollingFileAppender">
    <param name="datePattern" value="'.'yyyy-MM-dd_HH-mm" />
    <param name="file" value="logs/MyLogFile.log" />
    <param name="Append" value="true" />
    <layout class="org.apache.log4j.PatternLayout">
        <param name="ConversionPattern" value="%d{ISO8601} %-5p [%t] %c: %m%n" />
    </layout>
</appender>

root.xml:

<?xml version="1.0" encoding="UTF-8"?>
<root>
    <priority value="INFO" />
    <appender-ref ref="MyAppender" />
</root>

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:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd"[
    <!ENTITY config SYSTEM "log4j-config.txt">
]>
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/" >
&config;
</log4j:configuration>

log4j-config.txt:

<appender name="MyAppender" class="org.apache.log4j.DailyRollingFileAppender">
    <param name="datePattern" value="'.'yyyy-MM-dd_HH-mm" />
    <param name="file" value="logs/MyLogFile.log" />
    <param name="Append" value="true" />
    <layout class="org.apache.log4j.PatternLayout">
        <param name="ConversionPattern" value="%d{ISO8601} %-5p [%t] %c: %m%n" />
    </layout>
</appender>
<root>
    <priority value="INFO" />
    <appender-ref ref="MyAppender" />
</root>

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.

like image 189
tohuwawohu Avatar answered Oct 28 '22 07:10

tohuwawohu


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.

like image 26
FrVaBe Avatar answered Oct 28 '22 07:10

FrVaBe