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 "Brown, James" <ja...@ca.unisys.com> on 2004/04/06 13:40:36 UTC
PropertyConfigurator and Logger Factories
According to http://logging.apache.org/log4j/docs/api/org/apache/log4j/PropertyConfigurator.html#doConfigure(java.lang.String,%20org.apache.log4j.spi.LoggerRepository):
"The usage of custom logger factories is discouraged and no longer documented."
What is the alternative?
Much more detail ...
However, I have run into an instance where I need to utilize this mechanism. Otherwise I receive ClassCastExceptions when trying to set the level for classes via:
log4j.logger.class.of.the.day=[level]
I have defined a LoggerFactory as such:
package xyz;
public class LoggerFactory implements org.apache.log4j.spi.LoggerFactory {
/**
* Creates a new ca.acol.common.logging.Logger instance.
* @param name the name of the logger instance.
* @return an org.apache.log4j.Logger
*/
public org.apache.log4j.Logger makeNewLoggerInstance(String name) {
return new Logger(name);
}
}
And xyz.Logger is defined as:
package xyz;
public class Logger extends org.apache.log4j.Logger {
private static LoggerFactory xyzFactory = new LoggerFactory();
private LoggerContext loggerContext;
protected Logger(String name) {
super(name);
}
public static Logger getXYZLogger(String name) {
return (Logger) Logger.getLogger(name, acolFactory);
}
public static Logger getXYZLogger(Class clazz) {
return (Logger) Logger.getLogger(clazz.getName(), acolFactory);
}
public void setLoggerContext(LoggerContext logContext) {
loggerContext = logContext;
}
protected void forcedLog(String fqcn, Priority priority, Object message, Throwable t) {
if (loggerContext != null) {
NDC.push(loggerContext.getMessage());
}
super.forcedLog(fqcn, priority, message, t);
if (loggerContext != null) {
NDC.pop();
}
}
public LoggerContext getLoggerContext() {
return loggerContext;
}
}
If I have a class abc.MyClass:
package abc;
public class MyClass {
private static final Logger log =
xyz.Logger.getXYZLogger(MyClass.class);
.
.
.
}
If I attempt to put the following in the log4j.properties file, I receive a ClassCastException from xyz.Logger.getXYZLogger(Class clazz):
log4j.logger.xyz.MyClass=ERROR
Unless I also add the following:
log4j.loggerFactory=xyz.LoggerFactory
Any help would be greatly appreciated.
-- James
THIS COMMUNICATION MAY CONTAIN CONFIDENTIAL AND/OR OTHERWISE PROPRIETARY MATERIAL and is thus for use only by the intended recipient. If you received this in error, please contact the sender and delete the e-mail and its attachments from all computers.
---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-user-help@logging.apache.org