tags:

views:

637

answers:

2

I'm brand new with log4j and I can't seem to figure this out. If I set log4j.configuration to some garbage file that doesn't exist, the following program works as I expect. But if I don't then it's silent (other than printing out the properties):

package com.example.test;

import org.apache.log4j.*;

public class Log4jExample2 {
    final static Logger logger = Logger.getLogger("test.Log4jExample2"); 

    static void kvshow(String[] karray)
    {
     for (String k : karray)
     {
      String v = System.getProperty(k);
      System.out.println(k+"="+v);
     }
    }

    public static void main(String[] args) {
     kvshow(new String[] {"log4j.configuration", "log4j.properties"});
     BasicConfigurator.configure();
     logger.debug("Hello world.");
    }

}

Here's a runtime session:

>java -cp test-20090219.jar com.example.test.Log4jExample2
log4j.configuration=null
log4j.properties=null

>java -cp test-20090219.jar -Dlog4j.configuration=blah.cfg com.example.test.Log4jExample2
log4j.configuration=blah.cfg
log4j.properties=null
0 [main] DEBUG test.Log4jExample2  - Hello world.

I'm stumped. (again, there is no file called blah.cfg, this was the simplest way I could get things to work) Where is the log4j getting its instructions from?

A: 

The BasicConfigurator sets up default values for Log4J. So there is no reading from a configuration file when you use the BasicConfigurator. To configure from a properties file, use the PropertiesConfigurator

Rob Di Marco
no, you missed it. I'm not trying to read from a configuration file. I'm trying to run the BasicConfigurator examples, and something was suppressing the log output I expected.
Jason S
A: 

Hmm, I have a lead... I was looking through the log4j manual and randomly tried this:

>java -Dlog4j.debug=true com.example.test.Log4jExample2
log4j: Trying to find [log4j.xml] using context classloader sun.misc.Launcher$AppClassLoader@11b86e7.
log4j: Trying to find [log4j.xml] using sun.misc.Launcher$ExtClassLoader@35ce36 class loader.
log4j: Trying to find [log4j.xml] using ClassLoader.getSystemResource().
log4j: Trying to find [log4j.properties] using context classloader sun.misc.Launcher$AppClassLoader@11b86e7.
log4j: Using URL [jar:file:/C:/appl/Java/jre6u10/lib/ext/bsf.jar!/log4j.properties] for automatic log4j configuration.
log4j: Reading configuration from URL jar:file:/C:/appl/Java/jre6u10/lib/ext/bsf.jar!/log4j.properties
log4j: Parsing for [root] with value=[FATAL, CONSOLE].
log4j: Level token is [FATAL].
log4j: Category root set to FATAL
log4j: Parsing appender named "CONSOLE".
log4j: Parsing layout options for "CONSOLE".
log4j: End of parsing for "CONSOLE".
log4j: Parsed "CONSOLE" options.
log4j: Finished configuring.
log4j.configuration=null
log4j.properties=null

Looks like the bean scripting framework is overriding me.

How can I stop this? What I want is, if the JRE is not invoked explicitly with a properties file, then I want it to use the settings included in my .jar file. I tried putting my own log4j.properties file in the root of my .jar file but that doesn't seem to work.

edit: aha! I think I got it: I have to do a System.setProperty before the first use of the Logger.getLogger(), something like:

static
{
 System.setProperty("log4j.configuration",
 "jar:file:my_jar_file_name_here.jar!/log4j.properties");  
}
Jason S