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 Kaspar Fischer <fi...@inf.ethz.ch> on 2009/05/02 16:53:45 UTC

Log file per thread

Dear list,

I am looking for a way to create a log file per thread (actually, per  
Quartz job that gets run).

I've googled quite a bit and found a few approaches, in particular  
KeyFileAppender [1]. All I have seen seem to rely on the MDC and  
KeyFileAppender seems to have a resource leak [2].

I am wondering whether there is a simply way to programmatically  
create, at the beginning of my thread, a logger with a given name and  
closing it at the end of the thread. Has anybody accomplished  
something like this?

   public void run()
   {
      String id = ...; // thread name or whatever
      Logger logger = ...; // Create a logger with a basename and the id
      try
      {
        // ...
      }
      finally
      {
        logger.close(); // Something like this is needed to make sure
                        // old logs do not keep hanging around
      }
   }

Many thanks,
Kaspar

[1] https://issues.apache.org/bugzilla/show_bug.cgi?id=19597
[2] http://www.mail-archive.com/log4j-user@logging.apache.org/msg07295.html

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


Re: Log file per thread

Posted by Jacob Kjome <ho...@visi.com>.
What you have below does nothing for the file being appended to.  There have been
long discussions in the past, on this list, about a file per thread.  I believe
most have resorted to using a custom, thread-based, repository selector.  I
suggest research be done on what's been said before.

As far as disposing of loggers so they don't increase with the number of threads,
if they are named the same thing, then they get reused in the same logger
repository, so that wouldn't be an issue.  That is, assuming you found a way to
log to a different file/thread without resorting to a repository selector.

If you do use a repository selector, you'd probably have the issue of logger
repositories increasing as threads increase.  In that case, you'd probably need to
implement some logger repository disposal routine.


Jake

On 5/3/2009 8:43 AM, Yair Ogen wrote:
> Hi,
> 
> I can't say I tried this myself but let's assume you have this code:
> 
> public void run()
> {
>   String id = ...; // thread name or whatever
>   Logger logger = ...; // Create a logger with a basename and the id
>   ThreadLocal<Logger> loggerThreadLocal = new ThreadLocal<Logger>();
>   loggerThreadLocal.set(logger);
>   try
>   {
>     // ...
>     ((Logger)loggerThreadLocal.get()).info...
>   }
>   finally
>   {
>     // nothing here. when the thread dies the thread local is cleaned up for
> you.
>   }
> }
> 
> 
> well this is a pseudo of my idea. Again - haven't tried this...
> 
> good luck.
> 
> 
> 
> On Sun, May 3, 2009 at 3:04 PM, Kaspar Fischer <fi...@inf.ethz.ch> wrote:
> 
>> Yair, first of all, thanks for your answer.
>>
>>  Ho about creating a Logger in ThreadLocal with a unique name that is based
>>> on the thread name?
>>>
>> Apologies for this basic question, but how can I do this?
>>
>> Just passing a new name to getLogger()?
>>
>>  http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/Logger.html
>>
>> More importantly, how can I dispose of the logger? Otherwise the number of
>> open loggers grows with the number of threads run.
>>
>> Thanks,
>> Kaspar
>>
>>
>>  On Sat, May 2, 2009 at 5:53 PM, Kaspar Fischer <fi...@inf.ethz.ch>
>>> wrote:
>>>
>>>  Dear list,
>>>> I am looking for a way to create a log file per thread (actually, per
>>>> Quartz job that gets run).
>>>>
>>>> I've googled quite a bit and found a few approaches, in particular
>>>> KeyFileAppender [1]. All I have seen seem to rely on the MDC and
>>>> KeyFileAppender seems to have a resource leak [2].
>>>>
>>>> I am wondering whether there is a simply way to programmatically create,
>>>> at
>>>> the beginning of my thread, a logger with a given name and closing it at
>>>> the
>>>> end of the thread. Has anybody accomplished something like this?
>>>>
>>>> public void run()
>>>> {
>>>>   String id = ...; // thread name or whatever
>>>>   Logger logger = ...; // Create a logger with a basename and the id
>>>>   try
>>>>   {
>>>>     // ...
>>>>   }
>>>>   finally
>>>>   {
>>>>     logger.close(); // Something like this is needed to make sure
>>>>                     // old logs do not keep hanging around
>>>>   }
>>>> }
>>>>
>>>> Many thanks,
>>>> Kaspar
>>>>
>>>> [1] https://issues.apache.org/bugzilla/show_bug.cgi?id=19597
>>>> [2]
>>>> http://www.mail-archive.com/log4j-user@logging.apache.org/msg07295.html
>>>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
>> For additional commands, e-mail: log4j-user-help@logging.apache.org
>>
>>
> 

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


Re: Log file per thread

Posted by Yair Ogen <ya...@gmail.com>.
Hi,

I can't say I tried this myself but let's assume you have this code:

public void run()
{
  String id = ...; // thread name or whatever
  Logger logger = ...; // Create a logger with a basename and the id
  ThreadLocal<Logger> loggerThreadLocal = new ThreadLocal<Logger>();
  loggerThreadLocal.set(logger);
  try
  {
    // ...
    ((Logger)loggerThreadLocal.get()).info...
  }
  finally
  {
    // nothing here. when the thread dies the thread local is cleaned up for
you.
  }
}


well this is a pseudo of my idea. Again - haven't tried this...

good luck.



On Sun, May 3, 2009 at 3:04 PM, Kaspar Fischer <fi...@inf.ethz.ch> wrote:

> Yair, first of all, thanks for your answer.
>
>  Ho about creating a Logger in ThreadLocal with a unique name that is based
>> on the thread name?
>>
>
> Apologies for this basic question, but how can I do this?
>
> Just passing a new name to getLogger()?
>
>  http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/Logger.html
>
> More importantly, how can I dispose of the logger? Otherwise the number of
> open loggers grows with the number of threads run.
>
> Thanks,
> Kaspar
>
>
>  On Sat, May 2, 2009 at 5:53 PM, Kaspar Fischer <fi...@inf.ethz.ch>
>> wrote:
>>
>>  Dear list,
>>>
>>> I am looking for a way to create a log file per thread (actually, per
>>> Quartz job that gets run).
>>>
>>> I've googled quite a bit and found a few approaches, in particular
>>> KeyFileAppender [1]. All I have seen seem to rely on the MDC and
>>> KeyFileAppender seems to have a resource leak [2].
>>>
>>> I am wondering whether there is a simply way to programmatically create,
>>> at
>>> the beginning of my thread, a logger with a given name and closing it at
>>> the
>>> end of the thread. Has anybody accomplished something like this?
>>>
>>> public void run()
>>> {
>>>   String id = ...; // thread name or whatever
>>>   Logger logger = ...; // Create a logger with a basename and the id
>>>   try
>>>   {
>>>     // ...
>>>   }
>>>   finally
>>>   {
>>>     logger.close(); // Something like this is needed to make sure
>>>                     // old logs do not keep hanging around
>>>   }
>>> }
>>>
>>> Many thanks,
>>> Kaspar
>>>
>>> [1] https://issues.apache.org/bugzilla/show_bug.cgi?id=19597
>>> [2]
>>> http://www.mail-archive.com/log4j-user@logging.apache.org/msg07295.html
>>>
>>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
> For additional commands, e-mail: log4j-user-help@logging.apache.org
>
>

Re: Log file per thread

Posted by Kaspar Fischer <fi...@inf.ethz.ch>.
Yair, first of all, thanks for your answer.

> Ho about creating a Logger in ThreadLocal with a unique name that is  
> based
> on the thread name?

Apologies for this basic question, but how can I do this?

Just passing a new name to getLogger()?

   http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/Logger.html

More importantly, how can I dispose of the logger? Otherwise the  
number of
open loggers grows with the number of threads run.

Thanks,
Kaspar

> On Sat, May 2, 2009 at 5:53 PM, Kaspar Fischer  
> <fi...@inf.ethz.ch> wrote:
>
>> Dear list,
>>
>> I am looking for a way to create a log file per thread (actually, per
>> Quartz job that gets run).
>>
>> I've googled quite a bit and found a few approaches, in particular
>> KeyFileAppender [1]. All I have seen seem to rely on the MDC and
>> KeyFileAppender seems to have a resource leak [2].
>>
>> I am wondering whether there is a simply way to programmatically  
>> create, at
>> the beginning of my thread, a logger with a given name and closing  
>> it at the
>> end of the thread. Has anybody accomplished something like this?
>>
>> public void run()
>> {
>>    String id = ...; // thread name or whatever
>>    Logger logger = ...; // Create a logger with a basename and the id
>>    try
>>    {
>>      // ...
>>    }
>>    finally
>>    {
>>      logger.close(); // Something like this is needed to make sure
>>                      // old logs do not keep hanging around
>>    }
>> }
>>
>> Many thanks,
>> Kaspar
>>
>> [1] https://issues.apache.org/bugzilla/show_bug.cgi?id=19597
>> [2]
>> http://www.mail-archive.com/log4j-user@logging.apache.org/msg07295.html

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


Re: Log file per thread

Posted by Yair Ogen <ya...@gmail.com>.
Ho about creating a Logger in ThreadLocal with a unique name that is based
on the thread name?

On Sat, May 2, 2009 at 5:53 PM, Kaspar Fischer <fi...@inf.ethz.ch> wrote:

> Dear list,
>
> I am looking for a way to create a log file per thread (actually, per
> Quartz job that gets run).
>
> I've googled quite a bit and found a few approaches, in particular
> KeyFileAppender [1]. All I have seen seem to rely on the MDC and
> KeyFileAppender seems to have a resource leak [2].
>
> I am wondering whether there is a simply way to programmatically create, at
> the beginning of my thread, a logger with a given name and closing it at the
> end of the thread. Has anybody accomplished something like this?
>
>  public void run()
>  {
>     String id = ...; // thread name or whatever
>     Logger logger = ...; // Create a logger with a basename and the id
>     try
>     {
>       // ...
>     }
>     finally
>     {
>       logger.close(); // Something like this is needed to make sure
>                       // old logs do not keep hanging around
>     }
>  }
>
> Many thanks,
> Kaspar
>
> [1] https://issues.apache.org/bugzilla/show_bug.cgi?id=19597
> [2]
> http://www.mail-archive.com/log4j-user@logging.apache.org/msg07295.html
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
> For additional commands, e-mail: log4j-user-help@logging.apache.org
>
>