First:
For simple configuration needs (in fact for most configuration needs) you do
not need to call a configurator explicitely. Just put your config file in the
classpath, and log4j finds it for you and autoconfigures itself the first time
the LogManager is loaded (e.g. on a static Logger.getLogger()). Log4j looks
first for a file "log4j.xml", and if not found, for "log4j.properties". That's
all. (you can overwrite the name of the file by the commandline argument or by
system property).
Second:
You ran into some subtle problems which misleaded you to wrong assumptions.
- If for some reasons you must load the configuration programmatically: a XML
configuration should be loaded by the DOMConfigurator, a properties file by the
PropertiesConfigurator. I suppose in your second experiment the (default)
configurator finds the default.xml (given by commandline param), but the
PropertyConfigurator cannot load the second xml file (as in all further
experiments).
- the double output in your third experiment was due you have then two loggers
in the hierarchy (both from the default.xml). In code you are calling the
logger "Test" which outputs to its appender, then climbing up to the root
logger which outputs the same log message to its appender again (which happens
to be the same one). To avoid this, you should declare your Test-Logger with
additivity set to false (default true):
Third:
Multiple calls to XXXConfigurator.configure append new configuration items to
the already loaded ones unless calling LogManager.resetConfiguration(). When
you did this, the first configuration from the default.xml was purged and the
second one did not succeed (see above). That's the reason for the last WARN
messages.
Heri
-Ursprüngliche Nachricht-
Von: pagod [mailto:dvergn...@yahoo.com]
Gesendet: Dienstag, 29. September 2009 18:56
An: log4j-user@logging.apache.org
Betreff: Configuring log4j with XML
Hi everyone,
I seem to have a solid understanding problem with the way log4j loads its
configuration, perhaps someone can take a few minutes to explain it to me? I
only started a few hours ago, but I'm already startled... :-|
Basically, what I want to do is create a simple logger and configure it using
an XML file.
So my questions are:
- does log4j have to load a default configuration file (log4j.xml or whatever I
set log4j.configuration to) even if I call the PropertyConfigurator.configure(
String filename ) function??
- This wouldn't be such a problem if I understood clearly how the default
configuration file interacts with another configuration file I might be trying
to load: are the loggers/appenders simply added to the default configuration,
or are there any risks when overwriting parts of it?
A concrete example:
Here's the Java code I'm testing with:
import org.apache.log4j.*;
public class Test {
static Logger
__logger;
public static void main( String args[] ) throws Exception {
// 1
//PropertyConfigurator.configure( "~/log4j_config.xml" );
// 2
BasicConfigurator.configure();
__logger = Logger.getLogger( Test.class );
__logger.info( "bonjour" );
}
}
Pretty basic. Now I have the following file which is used as the default
configuration by specifying -Dlog4j.configuration=log4j_default.xml on the
command line when starting java:
http://logging.apache.org/log4j/docs/api/org/apache/log4j/xml/log4j.dtd"; >
Again pretty easy, and when I execute the program I get the expected output
(i.e. nothing).
Now suppose I have a second file called log4j_config.xml, which is the same as
log4j_default.xml, except that it has an additional entry:
After uncommenting the line after // 1 in my code and commenting out the one
after // 2 (i.e., as I understand it, switching from default configuration to
"named" configuration), I find that the normal output hasn't changed a single
bit, although I'd have expected a message "bonjour" to be dumped to the console
(appender "stdout" is registered for logger Test with level output, which
matches the code, doesn't it?). However, the debug output (enabled with
-Dlog4j.debug=1) has changed, below is the output after the first call (default
config) and, in bold, the two lines added after the second call (file config):
log4j: Trying to find [log4j_default.xml] using context classloader
sun.misc.launcher$appclassloa...@61ba34f2.
log4j: Using URL [file:/home/pagod/projects/Test/log4j_default.xml] for
automatic log4j configuration.
log4j: Preferred configurator class: org.apache.log4j.xml.DOMConfigurator
log4j: System property is :null
log4j: Standard DocumentBuilderFactory search succeded.
log4j: DocumentBuilderFactory is:
com.sun.org.apache.xerces