Go Back   Velocity Reviews > Newsgroups > Java
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply

Java - log4j multiple appenders

 
Thread Tools Search this Thread
Old 11-05-2009, 08:19 AM   #1
Default log4j multiple appenders


I'm logging hibernate HQL queries to a log file (file1.log).

The problem is I have a thread which logs to a different file (file2.log).

So I want to change appenders at runtime so that 2 different threads
would simultaniously write hibernate loggs to a different file.

I'm not sure if this will work because I don't know how log4j getLogger
is implemented (if it is singleton or not).
The idea is to call the code below as needed.

Does anyone have any suggestions about this?

Thanks in advance.



-------------------------------------------------------
public static void setHibernateLogAppender(Logger logger) {
Logger.getLogger("org.hibernate.SQL").removeAllApp enders();

while(logger.getAllAppenders().hasMoreElements())
Logger.getLogger("org.hibernate.SQL").addAppender( (Appender)logger.getAllAppenders().nextElement());
}
------------------------------------------------------


Uli Kunkel
  Reply With Quote
Old 11-05-2009, 02:35 PM   #2
Lew
 
Posts: n/a
Default Re: log4j multiple appenders
Uli Kunkel wrote:
> I'm logging hibernate [sic] HQL queries to a log file (file1.log).
>
> The problem is I have a thread which logs to a different file (file2.log).
>
> So I want to change appenders at runtime so that 2 different threads
> would simultaniously write hibernate [sic] loggs to a different file.
>
> I'm not sure if this will work because I don't know how log4j getLogger
> is implemented (if it is singleton or not).
> The idea is to call the code below as needed.
>
> Does anyone have any suggestions about this?


A few minutes with the log4j documentation shows that its loggers are
identified by the name given them, usually a class name. You configure
loggers hierarchically - one configured for your Hibernate client can have its
own destination regardless of thread by the choice of its name.

--
Lew


Lew
  Reply With Quote
Old 11-06-2009, 03:02 AM   #3
Arne Vajhøj
 
Posts: n/a
Default Re: log4j multiple appenders
Uli Kunkel wrote:
> I'm logging hibernate HQL queries to a log file (file1.log).
>
> The problem is I have a thread which logs to a different file (file2.log).
>
> So I want to change appenders at runtime so that 2 different threads
> would simultaniously write hibernate loggs to a different file.
>
> I'm not sure if this will work because I don't know how log4j getLogger
> is implemented (if it is singleton or not).
> The idea is to call the code below as needed.
>
> Does anyone have any suggestions about this?


> -------------------------------------------------------
> public static void setHibernateLogAppender(Logger logger) {
> Logger.getLogger("org.hibernate.SQL").removeAllApp enders();
>
> while(logger.getAllAppenders().hasMoreElements())
> Logger.getLogger("org.hibernate.SQL").addAppender( (Appender)logger.getAllAppenders().nextElement());
>
> }
> ------------------------------------------------------


Your problem is not easily solvable.

Logger.getLogger("org.hibernate.SQL") will under normal circumstances
return the same logger for both threads.

If the two threads are not logging at the same time, then
you may be able to change config at runtime (I have not checked
whether your code is good or not).

But it is not a good solution.

I think a better solution would be a custom appender
that are able to log to two files depending on threads
and then at runtime config that appender to match threads
with files.

That will allow concurrent usage.

Arne


Arne Vajhøj
  Reply With Quote
Old 11-06-2009, 08:13 AM   #4
Uli Kunkel
 
Posts: n/a
Default Re: log4j multiple appenders
Arne Vajhøj wrote:
> Uli Kunkel wrote:
>> I'm logging hibernate HQL queries to a log file (file1.log).
>>
>> The problem is I have a thread which logs to a different file
>> (file2.log).
>>
>> So I want to change appenders at runtime so that 2 different threads
>> would simultaniously write hibernate loggs to a different file.
>>
>> I'm not sure if this will work because I don't know how log4j
>> getLogger is implemented (if it is singleton or not).
>> The idea is to call the code below as needed.
>>
>> Does anyone have any suggestions about this?

>
>> -------------------------------------------------------
>> public static void setHibernateLogAppender(Logger logger) {
>> Logger.getLogger("org.hibernate.SQL").removeAllApp enders();
>>
>> while(logger.getAllAppenders().hasMoreElements())
>> Logger.getLogger("org.hibernate.SQL").addAppender( (Appender)logger.getAllAppenders().nextElement());
>>
>> }
>> ------------------------------------------------------

>
> Your problem is not easily solvable.
>
> Logger.getLogger("org.hibernate.SQL") will under normal circumstances
> return the same logger for both threads.
>
> If the two threads are not logging at the same time, then
> you may be able to change config at runtime (I have not checked
> whether your code is good or not).
>
> But it is not a good solution.
>
> I think a better solution would be a custom appender
> that are able to log to two files depending on threads
> and then at runtime config that appender to match threads
> with files.
>
> That will allow concurrent usage.
>
> Arne



I didn't have time to test my idea yet, but as I see it's not gonna
work. I'll check if the custom appender is relatively easy to implement..


Uli Kunkel
  Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Can't print multiple copies in XP but can in 2000 on a client/serv =?Utf-8?B?U3VibWl0MnM=?= Windows 64bit 1 04-17-2007 07:16 PM
Broadband ? Sam Computer Support 38 11-06-2004 03:02 AM
Re: Broadband ? Kadaitcha Man Computer Information 27 11-06-2004 03:02 AM
SYMANTEC SECURITY ADVISORIES sam1967@hetnet.nl Computer Security 2 02-14-2004 06:07 PM
Deluged with multiple copies, sometimes, when using yahoo mail to check POP mail dakota7 Computer Support 3 10-05-2003 10:31 PM




SEO by vBSEO 3.3.2 ©2009, Crawlability, Inc.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46