Skip to content
Advertisement

logging in interface methods

I have been working on java 7 so far and recently moved to java-8, one thing which was surprising is that you can add methods in java-8 interfaces.

So far so good….loved this new stuff!

Now, my problem is that logging is an essential part of any development but seems lombok.extern.slf4j won’t let you add log stuffs in by interface methods as it is only allowed on classes and enums.

How do you log your interface methods (if by lombok or is this the only way?? ) ? Or is interface methods not supposed to be logged? what am i missing here?

P.S : At present i am working with System.out.println…. yeah…thats noob 🙂

Advertisement

Answer

you can add logger to your interface manually, but your logger will be public:

public interface SomeInterface {
    Logger log = LoggerFactory.getLogger(SomIface.class);

    default void action() {
        log.info("TEST");
    }
}
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement