You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4j-user@logging.apache.org by Matthew Kemp <ma...@gmail.com> on 2007/07/19 23:29:50 UTC

Using multiple config files with log4j

I've spent a fair amount of time going through the mailing lists, so
hopefully this is not duplicate. What I want to do is fairly straightforward
conceptually. I want to be able to load log4j configuration from two (or
more) separate config files without using separate classloaders or similar
tricks, since I want both configs applied to a single LoggerRepository. The
reason for this is I have a base log4j configuration across all applications
plus some specific configurations for each application.

In this example let's say the two config files are: default_log4j.xml which
contains the basics like setting the logger level and attaching some
appenders to the logger "foo.bar" and app_log4j.xml which provides
application specific configuration (possibly adding more appenders to "
foo.bar").

Ideally the loading process would cascade so if default_log4j.xml was
(re)loaded then app_log4j.xml would also be loaded. However, if only
app_log4j.xml was (re)loaded default_log4j.xml would not be reloaded.

This is a fairly simple example with only two levels, but I believe this
could be used to cascade more levels of config. Is this possible at all or
is this going to be a major roll-your-own project?

Thanks,

Matthew Kemp

Re: Using multiple config files with log4j

Posted by Curt Arnold <ca...@apache.org>.
On Jul 20, 2007, at 10:31 AM, Matthew Kemp wrote:

> Thanks for your response.
>
> First, I didn't realize that the XML include mechanism worked with  
> log4j
> yet. I will give that a try and see if I can get it working. Do you  
> know if
> it should work with version 1.2.9 - I know the current version is  
> 1.2.14 but
> I'm stuck with the older version right now. Also, is the XML  
> provided for
> the app_log4j.xml file?
>

I'm pretty sure that relative file names on external entities were  
broken in log4j 1.2.9.   You may still be able to use it if you use  
absolute URL's for the external files (something like file:///home/ 
username/somedir/base_config.xml).

> Second, from reading the mailing list it seemed that if a logger  
> "foo.bar"
> was setup by one Configuration, then a second Configuration modified "
> foo.bar" in any way that all previous appenders would be closed and  
> removed.
> I just need to be able to guarantee that additional configuration  
> can be
> applied to a logger without affecting anything already done - I'm  
> about 100%
> sure that this case will occur in my setup.

That does seem to be the case.  So it appears that you would need to  
do substitution inside of each logger element instead of repeating  
logger elements.

Your best bet map to be parse your source documents into a  
org.w3c.dom.Element in your application and pass that to  
DOMConfigure.configure(Element).  You'd be free to do just about  
anything upstream as long as the DOM element looked like a standard  
log4j configuration when you got done.

---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-user-help@logging.apache.org


Re: Using multiple config files with log4j

Posted by Matthew Kemp <ma...@gmail.com>.
Thanks for your response.

First, I didn't realize that the XML include mechanism worked with log4j
yet. I will give that a try and see if I can get it working. Do you know if
it should work with version 1.2.9 - I know the current version is 1.2.14 but
I'm stuck with the older version right now. Also, is the XML provided for
the app_log4j.xml file?

Second, from reading the mailing list it seemed that if a logger "foo.bar"
was setup by one Configuration, then a second Configuration modified "
foo.bar" in any way that all previous appenders would be closed and removed.
I just need to be able to guarantee that additional configuration can be
applied to a logger without affecting anything already done - I'm about 100%
sure that this case will occur in my setup.

Matt

On 7/20/07, Curt Arnold <ca...@apache.org> wrote:
>
> You should have to options that should work out of the box.  I don't
> understand your needs enough to suggest which one would work better.
>
> First, DOMConfiguration.configure and PropertyConfigure.configure do
> not reset the configuration before loading the configuration file.
> This basically means that if you explicitly call configure for both
> files, it is as if you have combined them into one file.
>
> Second, XML has an built-in "include" mechanism called external
> entities.  Earlier versions of log4j had a problem that kept it from
> working, but you could use this to construct a configuration from
> multiple modules.  Something like:
>
> <!DOCTYPE log4j:configuration SYSTEM 'log4j.dtd' [
> <!ENTITY base_config SYSTEM 'base_config.xml'>
> ]>
> <log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
> <!--  include base configuration  -->
> &base_config;
> <!--  custom configuration goes here -->
> </log4j:configuration>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
> For additional commands, e-mail: log4j-user-help@logging.apache.org
>
>

Re: Using multiple config files with log4j

Posted by Curt Arnold <ca...@apache.org>.
You should have to options that should work out of the box.  I don't  
understand your needs enough to suggest which one would work better.

First, DOMConfiguration.configure and PropertyConfigure.configure do  
not reset the configuration before loading the configuration file.   
This basically means that if you explicitly call configure for both  
files, it is as if you have combined them into one file.

Second, XML has an built-in "include" mechanism called external  
entities.  Earlier versions of log4j had a problem that kept it from  
working, but you could use this to construct a configuration from  
multiple modules.  Something like:

<!DOCTYPE log4j:configuration SYSTEM 'log4j.dtd' [
<!ENTITY base_config SYSTEM 'base_config.xml'>
]>
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<!--  include base configuration  -->
&base_config;
<!--  custom configuration goes here -->
</log4j:configuration>

---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-user-help@logging.apache.org