Finally, we’ve got the new EasyLog version that supports “logging interfaces” - 1.2.5
Let’s see what we actually have now.
For example we have an interface Interface
and its implementation Implementation
package io.lenar.examples.model.interfaces;
public interface Interface {
String methodWeWantToLog(String param);
}
package io.lenar.examples.model.interfaces;
public class Implementation implements Interface {
public String methodWeWantToLog(String param) {
return "methodWeWantToLog result";
}
}
We know that we can annotate any method of the class Implementation
or the whole class with @LogIt
and log methods of the class.
Now starting the 1.2.5
version of EasyLog we can also annotate interfaces. The @LogIt
annotation will automatically apply for any implementation of the interface.
Log all methods of interface
@LogIt(label = "All implemented methods of Interface")
public class Implementation implements Interface {
public String methodWeWantToLog(String param) {
return "methodWeWantToLog result";
}
}
Log specific method of interface
public class Implementation implements Interface {
@LogIt(label = "methodWeWantToLog method of Interface")
public String methodWeWantToLog(String param) {
return "methodWeWantToLog result";
}
}
Enable logging for interfaces with EasyLog
I’m assuming you already know how to setup EasyLog in your project, if you don’t then take a look at the EasyLog site and EasyLog for Spring projects, EasyLog for non-Spring projects examples.
Once you setup EasyLog add that class to the folder where your EasyLog MyLogger
is placed
import io.lenar.easy.log.EasyLoggerExtension;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
@Aspect
public class LoggerExtension extends EasyLoggerExtension {
@Pointcut("execution(* io.lenar.examples..*+.*(..))")
public void anyMethodInPackage() {}
@Around("anyMethodInPackage()")
public Object method(ProceedingJoinPoint jp) throws Throwable {
return logIfMethodHasAnnotatedInterface(jp);
}
}
For Spring projects it’s almost the same
import io.lenar.easy.log.EasyLoggerExtension;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
@Component
@Aspect
public class LoggerExtension extends EasyLoggerExtension {
@Pointcut("execution(* io.lenar.examples.spring..*+.*(..))")
public void anyMethodInPackage() {}
@Around("anyMethodInPackage()")
public Object method(ProceedingJoinPoint jp) throws Throwable {
return logIfMethodHasAnnotatedInterface(jp);
}
}
Don’t forget to replace io.lenar.examples
or io.lenar.examples.spring
with your project package. It can be the root package or any specific package where your interfaces live. that will enable the @LogIt
annotation in that package and in all its sub-packages.
If you did everything right the logs will look like this
08:51:04.045 [main] INFO io.lenar.easy.log.UneasyLogger -
-> String public Implementation.methodWeWantToLog(String param)
param: "PARAMETER"
08:51:04.046 [main] INFO io.lenar.easy.log.UneasyLogger -
Execution/Response time: 0ms
<- String Implementation.methodWeWantToLog(String param)
"methodWeWantToLog result"
You may also find these posts interesting: