Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Two different logs with single log4j.properties file

 
Ravii Varma
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I'm trying to generate two different log files using single log4j.properties.

Log4j.properties:
log4j.appender.source1=DEBUG,sourceAppender1
log4j.additivity.source1=false
log4j.appender.sourceAppender1= org.apache.log4j.DailyRollingFileAppender
log4j.appender.sourceAppender1.DatePattern = '.'yyyy-MM-dd
log4j.appender.sourceAppender1.File = C:/sourceAppender1/logs/sourceAppender1.log
log4j.appender.sourceAppender1.layout = org.apache.log4j.PatternLayout
log4j.appender.sourceAppender1.layout.ConversionPattern = <%d{MMM dd}> %m%n

log4j.appender.source2=DEBUG,sourceAppender2
log4j.additivity.source2=false
log4j.appender.sourceAppender2= org.apache.log4j.DailyRollingFileAppender
log4j.appender.sourceAppender2.DatePattern = '.'yyyy-MM-dd
log4j.appender.sourceAppender2.File = C:/sourceAppender2/logs/sourceAppender2.log;
log4j.appender.sourceAppender2.layout = org.apache.log4j.PatternLayout
log4j.appender.sourceAppender2.layout.ConversionPattern = <%d{MMM dd}> %m%n

Java code:
//For source1
Logger logr1 = Logger.getLogger("source1");
DailyRollingFileAppender drf1 = (DailyRollingFileAppender)logr1.getAppender(sourceAppender1);


//For source2
Logger logr2 = Logger.getLogger("source2");
DailyRollingFileAppender drf2 = (DailyRollingFileAppender)logr2.getAppender(sourceAppender2);

With this setting, log files are getting generated in both source folders, but with duplicate logs, both logs have data for both the sources, which I don't want to happen.
Please suggest, where I'm doing wrong.

Thanks,
Ravs.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34396
346
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ravs,
Welcome to CodeRanch!

I think you are missing the part where you specify which package names go with which appender. See the section titled Appenders and Layouts. The tables show how they set up multiple appenders.

If this doesn't help, can you explain how you want to decide which logs go to which logger?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic