Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lombok @Slf4j and interfaces?

I am trying to add logging to my interface default method. For example:

@Slf4j // not allowed
interface MyIFace {
    default ThickAndThin doThisAndThat() {
        log.error("default doThisAndThat() called"); 
    }
}

Problem is that I get:

@Slf4j is legal only on classes and enums.

What would be the proper way to deal with this?

like image 638
pirho Avatar asked Nov 21 '25 00:11

pirho


1 Answers

You can't use Lombok here because the annotation generates:


private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(YourClass.class);

You can't use private keyword with an interface.

The workaround is to write directly


interface MyIFace {

   org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(MyIFace.class);

   default ThickAndThin doThisAndThat() {
     log.error("default doThisAndThat() called"); 
   }

}


like image 176
Andrei Kovrov Avatar answered Nov 22 '25 16:11

Andrei Kovrov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!