Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 :)

like image 515
NoobEditor Avatar asked Feb 13 '18 04:02

NoobEditor


2 Answers

Currently Lombok @Slf4j annotation is not supported on interfaces, but it can be circumvented like this

public interface MyInterface
{
    @Slf4j
    final class LogHolder
    {}

    default void action() {
        LogHolder.log.error("Error TEST");
    }
}
like image 108
Vaibhav Avatar answered Oct 06 '22 01:10

Vaibhav


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");
    }
}
like image 34
Alex M981 Avatar answered Oct 06 '22 00:10

Alex M981