Salut to all Java gurus!
Since Java8 we can have default implementations in interfaces (yay!). However problem arises when you want to log from default method.
I have a feeling that it is not wise to call .getLogger() every time I want to log something in a default method.
Yes, one can define static variable in an interface - but that is not a good practice for interfaces anyway + it exposes the logger (must be public).
Solution I have at the moment:
interface WithTimeout<Action> {
default void onTimeout(Action timedOutAction) {
LogHolder.LOGGER.info("Action {} time out ignored.", timedOutAction);
}
static final class LogHolder {
private static final Logger LOGGER = getLogger(WithTimeout.class);
}
}
LogHolder is still visible to everybody which doesn't really make any sense since it does not provide any methods and it should be internal to the interface.
Does any of you know about better solution? :)
EDIT: I use SLF4J backed by Logback