This library helps to handle method logging in Java.
- Java 1.8+
- Spring Framework
To start, add that Maven dependency:
<dependency>
<groupId>br.com.leverinfo</groupId>
<artifactId>aspectj-logging</artifactId>
<version>0.4.0</version>
</dependency>
or Gradle:
implementation 'br.com.leverinfo:aspectj-logging:0.4.0'
...configure a Spring Bean like this:
@Bean
public LoggingAspect getLogging() {
return new LoggingAspect();
}
then, in your method, add the @Logging
annotation:
public class Foo {
@Logging
public String sayAny(String word) {
return "Saying " + word;
}
}
The console shoud show something like this:
2023-04-29 14:53:23.611 DEBUG 25908 --- [ main] br.com.leverinfo.logging.Test : method=sayHello([word=Hello]) return=Saying Hello elapsedTime=1 ms
It is possible to configure which log level should to use for a given exception class.
For this, it is necessary configure the LoggingAspect
like this:
@Bean
public LoggingAspect getLogging() {
LogginAspect loggingAspect = new LoggingAspect();
loggingAspect.addExceptionLogLevel(MyException.class, Level.INFO);
return loggingAspect;
}
Logging annotation allows some configurations:
message
| Set a custom log message | Default:""
level
| Set which log level shoud be used | Default:Level.DEBUG
excludeParams
| Set the input parameters (index based) that shouldn't be logged. This is useful for not logging sensitive data, for example | Default:{}
logReturn
| Define if return value should be logged | Default:true
logError
| Define if errors should be logged | Default:true
logErrorTrace
| Define if complete stack trace should be logged. This property works togetherlogError
property, what means that iflogError
isfalse
, this property has no effect
- Currently, it works just with Spring Beans
- Make it works without Spring
- Create unit tests
- Configure CI/CD pipeline
Your contribution is appreciated.