You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4j-user@logging.apache.org by Michael Armida <mi...@bnw.com> on 2006/09/22 23:36:46 UTC

Subclassed Logger, LoggerFactory hits NullPointerException

Hello all,

I've just recently started working with log4j and the API, so I 
apologize for any blatant mistakes.  I promise that I've checked the bug 
reports and Googled around for the answer.

I have subclassed Logger and LoggerFactory, the subclasses are called 
TinderboxTestLogger and TinderboxLoggerFactory.  When I try to retrieve 
a new TinderboxTestLogger via this code:

   TinderboxTestLogger logger = (TinderboxTestLogger) 
Logger.getLogger(caller.getName(), new TinderboxLoggerFactory());

... I get a NullPointerException at Category.java:162.  This is because 
the logger's repository is null.  I've included the code for both 
classes below; you can see that 
TinderboxLoggerFactory.makeNewLoggerInstance() only assigns the new 
logger a Level, and that's about it.

The question: shouldn't I be attaching this logger to the heirarchy 
somehow?  Looking through the API, it's not jumping out at me how I 
attach the new TinderboxTestLogger to the repository or a parent logger; 
it seems this is controlled by the repository and RepositorySelector, 
which seems to be not part of the subclassable, extensible portion of 
the API.  I'm obviously doing something wrong in programmatically 
creating these loggers (instead of using one defined in the config file).

I'm using log4j 1.2.14 on Sun's jdk 1.5.0_08.

The big picture: I'm building some unit test automation stuff for 
TestNG; I'm providing log4j loggers to the test case code. 
TinderboxLogger just holds on to a StringWriter instance, which I use 
with WriterAppender to (hopefully) store log output as a vanilla String 
and eventually write to a database.

Any thoughts you might have on a much easier way to do all this are 
appreciated.

- Michael

*** TinderboxTestLogger.java ***

public class TinderboxTestLogger extends Logger {
	
	private StringWriter sw;
	
	public TinderboxTestLogger(String name) {
		super(name);
		sw = new StringWriter();
	}
	
	public StringWriter getStringWriter() {
		return sw;
	}
	
	public String getContents() {
		return sw.toString();
	}
}

*** TinderboxLoggerFactory.java ***

public class TinderboxLoggerFactory implements LoggerFactory {
	
	public TinderboxLoggerFactory() {
	}

	public Logger makeNewLoggerInstance(String name) {
		TinderboxTestLogger logger = new TinderboxTestLogger(name);
		logger.setLevel((Level) Level.ALL);
		WriterAppender wa = new WriterAppender(new SimpleLayout(), 
logger.getStringWriter());
		logger.addAppender(wa);
		return logger;
	}

}


*** log4j.properties ***

log4j.appender.file=org.apache.log4j.FileAppender
log4j.appender.file.File=/home/marmida/workspace/tinderbox/tmp/log/tinderbox.log
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - 
%m%n

log4j.rootLogger=DEBUG, file

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