How To Re-read log4j.properties

12 PM October 28, 2003

I had a peculiar problem this morning. log4j logging output was being turned off in a call to a third party library1. Not very much fun, particularly when exceptions start going unlogged.

Here are two code snippets that were useful for debugging the problem. This code resets the log4j configuration from the contents of the log4j.properties file:

static void resetLog4j() {
    LogManager.resetConfiguration();
    ClassLoader cl = this.getClass().getClassLoader();
    URL log4jprops = cl.getResource(
            "log4j.properties");
    if (log4jprops != null) {
        (new PropertyConfigurator())
                .configure(log4jprops);
    }
}

The log4j config can be dumped to standard out like this:

private void printLog4jConfig() {
    System.out.println("#log4j Config");
    PrintWriter pw = new PrintWriter(System.out);
    PropertyPrinter pp = new PropertyPrinter(pw);
    pp.print(pw);
    System.out.println("#End of Config");
}

1 Google turned up a solution to the same problem in a previous version of the product, but nothing for the current version. I couldn’t find any other mention in the Knowledge Base or Support forums. I submitted a bug report, but meanwhile I have a workaround.

By alang | # | Comments (9)
(Posted to javablogs and Java)

Comments

At 22:47, 05 Feb 2004 Simon Brunning wrote:

This entry is currently the number 1 hit for "log4j.properties". ;-)

(#)
At 12:12, 03 May 2004 P Q wrote:

you are an awesome geek!

(#)
At 13:30, 15 Sep 2004 christine wrote:

i wanted to know about cardboard, i guess this isnt the place to find it. but thanks anyways i learned alot about your problems in the morning. dont ask me why im looking for cardboard! school project. i dont think ill find it. i think im going to talk to the teacher about his stupid assignment. anyways thats my problem of the morning

(#)
At 13:22, 12 Jan 2005 misty wrote:

i needed the properties of cardboard but this ain't it so c ya u computer geeks good luck be in a loser!!!!!!!!!!!!!!!!!!!!!!!

peace

holl@ back

(#)
At 11:50, 02 Feb 2005 bob wrote:

Why are all of you so sad

(#)
At 11:56, 06 Jul 2005 john wrote:

nice blog. keep it up. visit us also at<a href="www.yahoo.com">john</a>

(#)
At 12:25, 15 Oct 2005 idntthinkso wrote:

boooooring!!!!!

(#)
At 20:31, 15 Aug 2007 GCB wrote:

This worked great for me. It reloads the log4j.properties file when a execute the code. That is all I needed.

(#)
At 13:53, 27 Nov 2007 ss wrote:

If I change the log4j.properties, will log4j reload it automatically?

(#)

Add Comment




(Not displayed)






(Leave blank line between paragraphs. URLs converted to links. HTML stripped. Indented source code will be formatted with <pre> tags.)




© 2003-2006 Alan Green