logging-log4j-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Matthew Kemp" <mattk...@gmail.com>
Subject Re: Log4j for Multiple classes
Date Tue, 13 Nov 2007 15:38:41 GMT
Loggers can be named anything you want them to be. Traditionally they're
named after the class to aid with debugging, etc. You don't have to
specifically create a logger for each class. If at the top of every class
you had a line like -

private static final Logger logger = Logger.getLogger("foo");

- then all your classes would share the logger "foo". As far as writing to a
file goes, appenders like FileAppender and DailyRollingFileAppender are what
actually create/write to files. There is a many to many relationship between
loggers and appenders. One logger might have many appenders or on appender
might be attached to many loggers.

If all you want is to have all classes log to the same file I'd stick with
the per class logger and setup a logger that is "com.foo" where com.foo is
the basis of your package hierarchy. Then attach your file appender to that
logger and all classes under com.foo will write to the file.

Hope that helps.

Matt



On Nov 13, 2007 8:47 AM, Tursun Wali <lordrisborik@gmail.com> 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
> GMT
>
>  John Hurt wrote:
> >
> > > Hi,
> > >
> > > My application is split up into multiple classes. If I had
> > > processing in
> > > each class that I wanted to log, what's the best way to make a
> > > reference to
> > > the Logger available, so that for instance they all write to the
> > > same log
> > > file?
> > >
> > > Do I create an instance of the Logger class at the beginning of the
> > > program
> > > flow, then pass that class to all the methods/classes that plan to do
> > > logging?
> > >
> > > Do I make a wrapper singleton class that has a Logger in it and all
> > > code
> > > refers to the wrapper class?
> > >
> > > Do I use a base class (or interface or abstract class) that includes a
> > > reference to a Logger class?
> > >
> > > Anyone has generic examples where Logger is used in more than 1
> > > class, how
> > > to use it gracefully? Thanks.
> ".
> that is exactly what I want . but I could not contact with him . that
> email address is not working .
> Could you please one if you give me good advice !!!
>
> Best Regards
> Tom
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
> For additional commands, e-mail: log4j-user-help@logging.apache.org
>
>

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message