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$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.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$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.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-unsubscr...@logging.apache.org For additional commands, e-mail: log4j-user-h...@logging.apache.org