logging-log4j-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Bender Heri <hben...@ergonomics.ch>
Subject AW: Configuring log4j with XML
Date Wed, 30 Sep 2009 07:12:51 GMT
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):
    <logger name="Test" additivity="false">
        <level value="info"/>
        <appender-ref ref="stdout" />
    </logger>

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:dvergnaud@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:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration PUBLIC "-//Apache//log4j LOG4J 1.0//EN"
"http://logging.apache.org/log4j/docs/api/org/apache/log4j/xml/log4j.dtd" > <log4j:configuration>
    <appender name="stdout" class="org.apache.log4j.ConsoleAppender">
        <layout class="org.apache.log4j.PatternLayout">
            
        </layout>
    </appender>
    <root>
        <level value="fatal" />
        <appender-ref ref="stdout" />
    </root>
</log4j:configuration>

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:
    <logger name="Test">
        <level value="info"/>
        <appender-ref ref="stdout" />
    </logger>

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$AppClassLoader@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.internal.jaxp.DocumentBuilderFactoryImpl
log4j: debug attribute= "null".
log4j: Ignoring debug attribute.
log4j: reset attribute= "false".
log4j: Threshold ="null".
log4j: Level value for root is  [fatal].
log4j: root level set to FATAL
log4j: Class name: [org.apache.log4j.ConsoleAppender]
log4j: Parsing layout of class: "org.apache.log4j.PatternLayout"
log4j: Setting property [conversionPattern] to [%d{ABSOLUTE} %5p %c{1}:%L - %m%n].
log4j: Adding appender named [stdout] to category [root].
log4j: Could not find root logger information. Is this OK?
log4j: Finished configuring.

What's this message about not finding the root logger information? It is contained in both
the default config file and the config file given to PropertyConfigurator.configure... 

And where's my info message??

Now however, if I add the same "Test" logger in my default config file and carry out exactly
the same command, I get a somewhat different output: 
log4j: Trying to find [log4j_default.xml] using context classloader sun.misc.Launcher$AppClassLoader@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.internal.jaxp.DocumentBuilderFactoryImpl
log4j: debug attribute= "null".
log4j: Ignoring debug attribute.
log4j: reset attribute= "false".
log4j: Threshold ="null".
log4j: Retreiving an instance of org.apache.log4j.Logger.
log4j: Setting [Test] additivity to [true].
log4j: Level value for Test is  [info].
log4j: Test level set to INFO
log4j: Class name: [org.apache.log4j.ConsoleAppender]
log4j: Parsing layout of class: "org.apache.log4j.PatternLayout"
log4j: Setting property [conversionPattern] to [%d{ABSOLUTE} %5p %c{1}:%L - %m%n].
log4j: Adding appender named [stdout] to category [Test].
log4j: Level value for root is  [fatal].
log4j: root level set to FATAL
log4j: Adding appender named [stdout] to category [root].
log4j: Could not find root logger information. Is this OK?
log4j: Finished configuring.
18:34:49,771  INFO Test:13 - bonjour
18:34:49,771  INFO Test:13 - bonjour

Now I get a message that my Test logger has actually been registered -- oh joy! However, at
the end I get two messages :-?
Could it be that I now actually have two loggers with the name "Test"?? And why didn't the
debug output tell me about my Test logger during the previous run? I can't be that log4j hadn't
even tried to load the file -- specifying the name of a non-existing file leads to a FileNotFoundException,
as one might have expected... 

So, could anyone tell me what it is I have so blatantly misunderstood? 

Just for fun, I've tried calling LogManager.resetConfiguration() before calling PropertyConfigurator.configure(
String filename ), and then upon calling the "info" function the following warnings are displayed:
log4j:WARN No appenders could be found for logger (Test).
log4j:WARN Please initialize the log4j system properly.

As it looks, my configuration file hasn't had much of an effect, although it defines the same
as the default config... :-|

I'd be so grateful if anyone could explain this to me!! 

Cheers,

pagod
--
View this message in context: http://www.nabble.com/Configuring-log4j-with-XML-tp25666820p25666820.html
Sent from the Log4j - Users mailing list archive at Nabble.com.


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


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


Mime
View raw message