log4j multiple appenders

U

Uli Kunkel

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").removeAllAppenders();

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

Lew

Uli said:
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.
 
A

Arne Vajhøj

Uli said:
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").removeAllAppenders();

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
 
U

Uli Kunkel

Arne said:
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..
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,190
Latest member
ClayE7480

Latest Threads

Top