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 Vikas Jaitly <vi...@aztec.soft.net> on 2004/09/29 08:05:01 UTC

Help needed for logging to multiple files !!!

Hi All,
I am trying to use log4j for my java application, and am stuck with a
problem. I have already tried a search on the archive but could not find a
solution for the current problem. Please provide me with a solution or point
me to the thread if it has already been discussed.
 
Problem statement
----------------------
I have a java application(not a *web* application), where multiple tasks can
be performed at a time .I want to log all the output to a file by name
<taskname>.log for each of the running tasks. Since the task name is not
available until runtime, I cannot configure it using the log4j.xml file.
 
One of the solutions I tried was to have a logger (as instance variable) for
each task having a name "com.mypackage.task.<taskname>. During the
initialisation I add a fileappender to the logger. Till now every thing
works perfectly, and I can see the log messages for different tasks in
different files.
 
The problem starts, if the task is stopped and started again during the same
session. For each new creation ofthe same task, the log mesage starts
getting duplicated in the same file.
 
Following are my questions :
1) Is the approach that I am following correct( having separate logger
instance for each task)? or is there a better solution ?
 
2) Is there a way to remove appender (or is it better to remove the logger
itself, once the task gets completed) I could not find a method to remove a
logger:-(
 
3) How is the performance effected if I have multiple loggers registered
(using logger.getLogger()) but do not remove it. During peak performance, I
could have hundreds of tasks running in parallel.
 
4) Does it help if I have each task running in a separate thread, so that we
have a logger setup for a thread (possibly get some info from the thread
local and initialize based on it). I am asking this because, I can foresee
my application to grow to a multithreaded application soon.
Any pointers on using such a approach ?
 
5) Can the same be achieved using some kind of filter ? 
 
Thanks in advance,
 
Regards,
Vikas
 
 

RE: Help needed for logging to multiple files !!!

Posted by Paul Smith <ps...@aconex.com>.
> Problem statement
> ----------------------
> I have a java application(not a *web* application), where
> multiple tasks can
> be performed at a time .I want to log all the output to a file by name
> <taskname>.log for each of the running tasks. Since the task name is not
> available until runtime, I cannot configure it using the log4j.xml file.

Do you _have_ to have a separate log file per task?  If I could show you how
all logs for all tasks went into the one file, but you could EASILY
distinguish one tasks logs from another, would that help you?

If yes, then I have a very quick and painless solution for you.  It will
even work when you go multithreaded.

cheers,

Paul Smith


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