aspose file tools*
The moose likes Other Open Source Projects and the fly likes Configure two log4j loggers in one application Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Products » Other Open Source Projects
Bookmark "Configure two log4j loggers in one application" Watch "Configure two log4j loggers in one application" New topic
Author

Configure two log4j loggers in one application

Leon Pu
Greenhorn

Joined: Nov 02, 2005
Posts: 22
Hi all,

I have two log4j logger classes in one application, there are two configuration files for the two loggers.

But I found that some time only one logger works, another logger's log writes to another logger.

Since these two loggers are working in one JVM, is it mean that only one log4j logger is available in one JVM?


Best regards,
Leon
[ December 11, 2005: Message edited by: Leon Pu ]
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18669
    
    8

You can have as many loggers as you like. It's the appenders that you define in your configuration, not the loggers. But the log4j system only has one central point of configuration, so having more than one configuration file doesn't make sense.
Leon Pu
Greenhorn

Joined: Nov 02, 2005
Posts: 22
There are two sub projects need logger, each sub project will be compiled to one jar file.

I hope one project's logger class can write to one file, and another project's logger class write to another file. The level and other configuration for the appender are same.

Is it possible to configure the log4j xml configuration file to receive certain logger class?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18669
    
    8

If you want to be logging to two different files, then you need two different appenders to log to those files. I don't know what you mean by a "logger class" but you can certainly configure a specific logger to use a specific appender.
Leon Pu
Greenhorn

Joined: Nov 02, 2005
Posts: 22
Logger class is my wrapper class to hide log4j from the invoker.

Could you give a sample to get logger with appender?
Leon Pu
Greenhorn

Joined: Nov 02, 2005
Posts: 22
Could anybody give a hint to get Logger by appender?

Thanks.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18669
    
    8

I don't even know what that means. If I guess what it might mean, I have no idea why you would want to do that anyway. Or are you asking how to configure log4j?
Leon Pu
Greenhorn

Joined: Nov 02, 2005
Posts: 22
Hi Paul,

thank you for your reply.

I have two services in one application. In IDE environment, they are two sub projects, and will be compiled to two jars. So I want to save their logs to different files.

The problem is although they are separate projects, they will be deployed to one war. They will work in same JVM.

I have defined their own appender for different log file output in xml configuration file. My question is how can I get the instance of org.apache.log4j.Logger which uses the certain appender for different service?


Best regards,
Leon
Padma Lalwani
Ranch Hand

Joined: Nov 02, 2004
Posts: 49
In log4j.xml, create two appenders e.g. service1 and service2, writing to two different files. Then add package level filter to each appender

<logger name="com.company.service1">
<appender-ref ref="service1"/>
</logger>

<logger name="com.company.service2">
<appender-ref ref="service2"/>
</logger>

Padma
Leon Pu
Greenhorn

Joined: Nov 02, 2005
Posts: 22
Originally posted by Padma Lalwani:
In log4j.xml, create two appenders e.g. service1 and service2, writing to two different files. Then add package level filter to each appender

<logger name="com.company.service1">
<appender-ref ref="service1"/>
</logger>

<logger name="com.company.service2">
<appender-ref ref="service2"/>
</logger>

Padma


It works. But I met a problem that one logger doesn't log the first time log, later it works without problem.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Configure two log4j loggers in one application