suppose I have a common class with logger initialized by its name:
public class SomeCalculationLogic {
private static final Log log = LogFactory.getLog(SomeCalculationLogic .class);
public void doCalculation() {
log.info("doing calculations...");
...
}
}
This class is used by two logically different flows - say it is called from classes "BackgroundTask" and "UserRequest". How to make output of SomeCalculationLogic's logger redirected to different log files (like background.log and main.log) depending on what calling class is?
I see three possible ways:
A working implementation of a subclassing solution would be something like this:
public class SomeCalculationLogic {
protected abstract Log getLog();
public void doCalculation() {
getLog().info("doing calculations...");
...
}
}
public class BackgroundCalculationLogic extends SomeCalculationLogic {
private static Log log = LogFactory.getLog(BackgroundCalculationLogic.class);
protected Log getLog() {
return log;
}
}
public class UserRequestCalculationLogic extends SomeCalculationLogic {
private static Log log = LogFactory.getLog(UserRequestCalculationLogic.class);
protected Log getLog() {
return log;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With