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 Mi...@CSE-CST.GC.CA on 2003/01/15 23:50:16 UTC

Dynamically creating loggers and appenders

Hi,

I am fairly new to log4j.  So far I have been able to use it successfully.
But now I want to do something a bit more complex and haven't been able to
figure out how.  I have searched the mail archive without success.  So here
goes ...

I have a properties file in which I define a "base" Logger along with an
associated DailyRollingFileAppender.  The properties file is read by a
static method in the class that will be doing the logging:
--> PropertyConfigurator.configureAndWatch(XYZ.properties).

Assume that the appender associated with my base logger writes to the file
named "c:\baseLogFile".  When I log info, I really want to log messages to
"several" files, according to data that is only available at run time.  For
example, if the messages have to do with fruit, then I might want to write
to files named baseLogFile.banana, baseLogFile.apple and baseLogFile.orange.

My understanding is that I need to create a new logger and appender for each
distinct file in which I want to log messages.  It is a simple matter to
create the child logger at run time since (1) the base logger is associated
with the class doing the logging and (2) the extension required to create
the child logger is information that is available at run time.  The problem
is in dynamically creating/associating a new appender for each new child
logger.  I don't necessarily know all the potential child loggers that will
be required.  Also, I want to keep the properties file simple (of course I
would rather not include any child logger specific info at all ... since I
don't know what they will be in advance).

Is it possible to (1) ask a child logger for its parent's appender(s), (2)
use the parent's appender to create a new appender for the child logger, (3)
change the File attribute of the new appender so that it refers to the new
file in which I want to write, then (4) associate the new appender to the
child logger?

Has anyone tried to do something similar in the past?  Can anyone suggest a
course of action?  Thanks in advance :-)

Mike

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>