Hello Darren,

This is from the log4j book (soon to be published). I hope it helps:

Reloading of a configuration file or reconfiguration of log4j from a
different configuration file is allowed and is also thread safe. The
crucial point to remember is that invoking any of the log4j
configurators does not reset the previous configuration however
reconfiguration has obviously some effect on the existing
configuration. In particular, all appenders of any logger explicitly
mentioned in the new configuration will be closed and removed from
that logger. Appenders attached to loggers not mentioned in the new
configuration file remain untouched. If an appender is attached to
multiple loggers, then it is possible for the appender to be closed
during the reconfiguration but remain attached to a logger not
mentioned in the second configuration file. In this case, log4j will
warn you about trying to log to a closed appender.

First configuration file

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">

<log4j:configuration xmlns:log4j='http://jakarta.apache.org/log4j/'>

   <appender name="A1" class="org.apache.log4j.FileAppender">
     <param name="File" value="A1.log">
     <layout class="org.apache.log4j.PatternLayout">
       <param name="ConversionPattern" value="%r %p [%t] %c - %m%n"/>
     </layout>
   </appender>

   <appender name="A2" class="org.apache.log4j.FileAppender">
     <param name="File" value="A2.log">
     <layout class="org.apache.log4j.PatternLayout">
       <param name="ConversionPattern" value="%r %p [%t] %c - %m%n"/>
     </layout>
   </appender>

   <logger name="com.foo">
     <appender-ref ref="A2" />
   </logger>

   <logger name="com.wombat">
     <appender-ref ref="A2" />
   </logger>

   <root>
     <priority value ="debug" />
     <appender-ref ref="A1" />
   </root>

</log4j:configuration>

The first configuration file defines an appender A1 attached to the
root logger, a second appender A2 is attached to loggers "com.foo" and
"com.wombat".

Second configuration file:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">

<log4j:configuration xmlns:log4j='http://jakarta.apache.org/log4j/'>

   <appender name="A1" class="org.apache.log4j.FileAppender">
     <param name="File" value="A1.log">
     <layout class="org.apache.log4j.PatternLayout">
       <param name="ConversionPattern" value="%r %p [%t] %c - %m%n"/>
     </layout>
   </appender>

   <logger name="com.foo">
     <level value=WARN">
   </logger>

   <root>
     <priority value ="debug" />
     <appender-ref ref="A1" />
   </root>

</log4j:configuration>

When the second configuration file is read by the DOMConfigurator,
since the root logger is mentioned in the second file, all the
appenders in the root are closed and then removed. A new appender
called A1 is instantiated, configured and attached to root.  Logger
"com.foo" is mentioned in the second configuration file. Consequently,
A2 will be closed and removed from "com.foo". However, it will remain
attached to "com.wombat". Trying to log with "com.wombat" logger will
cause log4j to emit a warning.


At 16:01 11.03.2002 -0800, Gemoets, Darren wrote:
>Hello all.  I'm a new log4j user and ran into a problem setting up my
>configuration from multiple (two) files.  I was wondering if anyone had
>any thoughts/suggestions.
>
>What I'd like to do is set "default" configuration from one file (preferably
>referred to by -Dlog4j.configuration), and then read additional
>configuration
>from another properties file which overrides the defaults.  The
>JavaDoc for PropertyConfigurator.doConfigure(String configFileName,
>Hierarchy hierarchy), says, "Read configuration from a file. The existing
>configuration is not cleared nor reset."  Therefore, it looked like I could
>do something like:
>
>   PropertyConfigurator props = new PropertyConfigurator();
>   // load default configuration
>   props.doConfigure("defaultConfig.txt", Category.getRoot().getHierarchy());
>   // load (optional) configuration which overrides specific fields of
>default
>   props.doConfigure("overrideConfig.txt",
>Category.getRoot().getHierarchy());
>
>So if my "defaultConfig.txt" looked like
>   # Set root category priority to ERROR and its only appender to Console.
>   log4j.rootCategory=ERROR, Console
>   # Console appender is set to be a ConsoleAppender.
>   log4j.appender.Console=org.apache.log4j.ConsoleAppender
>   # Console uses PatternLayout.
>   log4j.appender.Console.layout=org.apache.log4j.PatternLayout
>   log4j.appender.Console.layout.ConversionPattern=%-4r [%t] %-5p %c{2} %x -
>%m%n
>
>then I could simply add
>   log4j.rootCategory=DEBUG, Console
>
>to change the default logging from ERROR to DEBUG.  Because the existing
>configuration is neither cleared nor reset by doConfigure(), I figured I'd
>just be adding/changing existing config properties.  However, it doesn't
>seem
>to work that way.  It keeps reading the first file, then trying to read the
>second file and failing saying no appenders could be found for root, as if
>it cleared/reset the default configuration initially read.
>
>Thanks in advance for any help/suggestions!  I'm not on the log4j-user
>group,
>so please reply to [EMAIL PROTECTED] (and/or to the group).
>
>Regards,
>----------------
>Darren Gemoets
>Aquilent (formerly Commerce One e-Government Solutions)
>[EMAIL PROTECTED]
>301-496-1796
>
>--
>To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
>For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

--
Ceki


--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to