• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Configure two log4j loggers in one application

 
Leon Pu
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21322
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21322
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could anybody give a hint to get Logger by appender?

Thanks.
 
Paul Clapham
Sheriff
Posts: 21322
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic