logging-log4j-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Curt Arnold <carn...@apache.org>
Subject Re: Log4j for Multiple classes
Date Tue, 13 Nov 2007 16:49:22 GMT

On Nov 13, 2007, at 8:47 AM, Tursun Wali wrote:

> Dear all,
> I want to write a logger for my project . log file for single class is
> Ok . log for  multiple classes seems problem to me .  I do not want to
> allocate  one logger for one class , 10 logger for 10 class. I want to
> some other smart way .
> There was a post in mailing list by John  (johnh...@gmail.com
> ) . tile is   "How to use Log4J in Multiple
> classes? App Design?             Date     Tue, 12 Jul 2005 23:57:43

The number of loggers used in an application does not bear a direct  
correspondence to the number of "log files" produced by the  
application.   Number of loggers do not necessarily have a  
correspondence to the number of classes in an application, however  
the most common usage pattern is to associate distinct loggers for  
each class.

Think of loggers as a topic of conversation or a channel on an audio  
mixing board (like mic 1 or instrument 7).  The configuration  
determines what topics are followed and where they are eventually  
output.  Using one logger is like having an entire band standing  
around one microphone, you can turn the volume up or down on  
everybody, but you can't adjust the volume of individual performers.   
Having many loggers is like having many microphones where you can  
adjust the volume, mute the channel or record the performers separately.

appenders (your "log files") are like recording devices attached to a  
mixing board.  You could mix all the channels down to a one-channel  
recorder, record each channel independently or many intermediate  

Each individual logger in a hierarchy (like a channel in a mixer) is  
cheap to construct.   It is a very common usage pattern even in large  
applications to have an individual logger per class and has not been  
an issue.

If you really want to only have "one logger", just change the  
initialization of the logger instances from:

public class MyClass {
     private static Logger logger = Logger.getLogger(MyClass.class);


public class MyClass {
     private static Logger logger = Logger.getRootLogger();

However, I see that as an unwise optimization, since you lose the  
ability to independently control the logging of MyClass.

Until you have something more specific clearly in mind, attach a  
single FileAppender to the root logger (effectively mixing all the  
channels down to one recording) using a configuration file.  That  
would allow you to get more creative in your mixing later without  
modifying your application.

Hope this discussion clears up a few things and rereading the  
documentation makes things clearer.

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

View raw message