On 2005-05-05, Niclas Hedhman wrote:
> If the addChildLogger is in the ULogger interface, it could be a
> recommended pattern to do;
>
> public class MyClass
> {
> private MyOtherClass m_other1;
> private MyOtherClass m_other2;
>
> public MyClass( ULogger logger )
> {
> ULogger child1 = logger.addChildLogger( "other1" );
> m_other1 = new MyOtherClass( child1, 10 );
> ULogger child2 = logger.addChildLogger( "other2" );
> m_other2 = new MyOtherClass( child2, 20 );
> }
> }
>
> in which case the IoC principle is propagated to POJOs, and will
> greatly help in unittesting.
Consider the LogHelper class:
public class LogHelper {
public static Logger addChildLogger(Logger parent, String childName) {
// SEP is some suitable separator, e.g. "."
return LoggerFactory.getLogger(parent.getName()+SEP+childName);
}
}
In terms of LogHelper, MyClass can be rewritten as:
import org.some.package.LogHelper;
public class MyClass {
private MyOtherClass m_other1;
private MyOtherClass m_other2;
public MyClass( ULogger logger ) {
ULogger child1 = LogHelper.addChildLogger(logger, "other1" );
m_other1 = new MyOtherClass( child1, 10 );
ULogger child2 = LogHelper.addChildLogger(logger, "other2" );
m_other2 = new MyOtherClass( child2, 20 );
}
}
Admittedly, this is not as nice as Niclas' initial version. However,
please note that while log4j loggers can act as a factory, not all
implementations of o.slf4j.Logger have knowledge of their factory. In
other words, not all logger implementations can implement the
addChildLogger() factory method without going through hoops.
Maybe the underlying question is compatibility of a static
LoggerFactory class with IoC containers.
--
Ceki Gülcü
The complete log4j manual: http://www.qos.ch/log4j/
_______________________________________________
dev mailing list
[email protected]
http://slf4j.org/mailman/listinfo/dev