What about writing something like this in your log4j.xml
<appender name="main-logfile" class="org.apache.log4j.RollingFileAppender">
<param name="file" value="${user.home}/.logs/main.log"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="conversionPattern" value="%d %-5p %c{1}: %m%n"/>
</layout>
</appender>
<logger name="com.calculationlogic.main" additivity="false">
<appender-ref ref="main-logfile"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="conversionPattern" value="%d %-5p %c{1}: %m%n"/>
</layout>
</logger>
<appender name="background-logfile" class="org.apache.log4j.RollingFileAppender">
<param name="file" value="${user.home}/.logs/background.log"/>
</appender>
<logger name="com.calculationlogic.background" additivity="false">
<appender-ref ref="background-logfile"/>
</logger>
and then the java part:
public class SomeCalculationLogic {
private final Category logger;
public SomeCalculationLogic(final Category logger){
this.logger = logger;
}
public void doCalculation() {
log.info("doing calculations...");
}
}
public class BackgroundTask{
public BackgroundTask(){
new SomeCalculationLogic(Logger.getLogger("com.calculationlogic.background")).doCalculation()
}
}
public class MainTask{
public MainTask(){
new SomeCalculationLogic(Logger.getLogger("com.calculationlogic.main")).doCalculation()
}
}
The result is:
in background.log:
2010-06-25 14:23:35,496 INFO background: doing calculations...
in main.log:
2010-06-25 14:23:35,495 INFO main: doing calculations...