To force log4j to output where it's getting its logging settings from, add:
-Dlog4j.debug

Log4j manual: http://logging.apache.org/log4j/docs/manual.html

To log something with log4j do something like:

org.apache.log4j.Logger.getLogger(getClass()).error("");
org.apache.log4j.Logger.getLogger(getClass()).error("blah");
org.apache.log4j.Logger.getLogger(getClass()).error("",e);
org.apache.log4j.Logger.getLogger(getClass()).error("blah",e);
where e is an Exception and "blah" is a message you specify.

If you just call

org.apache.log4j.Logger.getLogger(getClass()).error(e);
then it will NOT log the stack trace.

Adding something like:

-Dlogj4.configuration=file:/J:/resin/lib/log4j.properties\\
will theoretically force log4j to use a particular log4j properties file. In practice it doesn't seem to actually work.

Forcing explicit initializing of log4j in webapp

Webapps sometimes fail to initialize log4j properly because log4j looks for the log4j.properties file after the app server is started but before the webapp is loaded. Here's a way to force it. Just call LoggingInit.doInit() in a static section of a globally used class. Of course, you could also add checking to ensure that multiple calls only do the loading once. That way you could call it from static sections of a a bunch of different code to ensure it's loaded.

package com.something.snazzy;

import java.io.IOException;
import java.util.Properties;
import org.apache.log4j.PropertyConfigurator;

public class LoggingInit
{
	public static void doInit() {
		try {
			Properties props = new Properties();
			props.load( XferGateway.class.getResourceAsStream("../../../log4j.properties") );
			PropertyConfigurator.configure(props);
		} catch(IOException e) {
			throw new RuntimeException(e);
		}
	}
}
Version 6.1 last modified by Geoff Fortytwo on 11/07/2010 at 19:22

Attachments 0

No attachments for this document
Website Top
Send Me Mail!:
   g42website4 AT g42.org
My Encyclopaedia Blog

Creator: Geoff Fortytwo on 2008/05/12 01:16
Copyright 2004-2007 (c) XPertNet and Contributing Authors
1.3.2.9174