[slf4j-user] Mix final non-static logger with a kind of SingletonLoggerFactory

Ceki Gulcu ceki at qos.ch
Mon Nov 2 20:12:12 CET 2009


Hello Florian,

You probably don't have 10'000 classes but 10'000 *instances* of a class or a 
few classes.

Anyway, you don't have to through a factory because the SLF4J factory will 
always return the same instance. Do read [1] for more info on this topic.

[1] http://logback.qos.ch/manual/loggingSeparation.html

Cheers,

Florian Huonder wrote:
> Hi all,
> 
>  
> 
> I have a little question how to use slf4j correctly.
> 
>  
> 
> We use SLF4J as logging frontend in a framework.
> 
> There are lots of classes that contain business logic. These classes are 
> initialized once.
> 
> Here we have the following signature for the loggers  “private final 
> Logger logger = LoggerFactory.getLogger(MyClass.class);“.
> 
> On the other hand there are a lot of domain classes. In a productive 
> environment it is possible that there are 10'000 or 100’000s of domain 
> classes.
> 
> The domain classes also have to log stuff. I think there it won’t be a 
> good idea to have the logger static.
> 
> But then the drawbacks of static occur (not ioc friendly, unknown 
> behavior because the logger is JVM wide the same).
> 
>  
> 
> We thought about the following solution but are not sure if SLF4J can be 
> used like that :
> 
> All business-logic classes that are initialized once get the logger like 
> written above :“private final Logger logger = 
> LoggerFactory.getLogger(MyClass.class);“.
> 
> The domain classes are created through a factory. In this factory we 
> have a call like “private final Logger logger = 
> MySingletonLoggerFactory.getLogger(MyDomainClass.class) ;“
> 
> The MySingletonLoggerFactory class then is responsible of returning the 
> same logger for each type of domain object.
> 
>  
> 
> What do you think about the solution?
> 
> Is this a feasible way ? Are there better ways to go?
> 
>  
> 
> I am really looking forward to hearing from you.
> 
>  
> 
> Regards,
> 
> Florian
> 

-- 
Ceki Gülcü
Logback: The reliable, generic, fast and flexible logging framework for Java.
http://logback.qos.ch



More information about the slf4j-user mailing list