This week's giveaway is in the Spring forum.
We're giving away four copies of REST with Spring (video course) and have Eugen Paraschiv on-line!
See this thread for details.
The moose likes Other Open Source Projects and the fly likes Two different logs with single file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Products » Other Open Source Projects
Bookmark "Two different logs with single file" Watch "Two different logs with single file" New topic

Two different logs with single file

Ravii Varma

Joined: Jun 17, 2012
Posts: 1

I'm trying to generate two different log files using single
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.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.

Jeanne Boyarsky
author & internet detective

Joined: May 26, 2003
Posts: 32815

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?

[OCA 8 book] [Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2
I agree. Here's the link:
subject: Two different logs with single file
jQuery in Action, 3rd edition