Logging
Module for declarative logging of method arguments and result using annotations.
Dependency¶
Most likely already transitively connected from other dependencies or from Logback, otherwise it needs to be added:
Dependency build.gradle:
Module:
Dependency build.gradle.kts:
Module:
Logging¶
It is expected to use special combinations of annotations to customize method logging.
Argument¶
@Log.in
public String methodWithReturnAndOnlyLogArgs(@Log.off String strParam,int numParam) {
return"testResult";
}
| Logging Level | Logging |
|---|---|
| TRACE, DEBUG |
DEBUG [] r.t.e.e.Example.methodWithArgs: > {data: {numParam: "4"}} |
| INFO |
INFO [] r.t.e.e.Example.methodWithArgs: > |
Result¶
@Log.out
public String methodWithOnlyLogReturnAndArgs(String strParam, int numParam) {
return "testResult"
}
| Logging level | Logging |
|---|---|
| TRACE, DEBUG |
DEBUG [] r.t.e.e.Example.methodWithArgs: < {data: {out: "testResult"}} |
| INFO |
INFO [] r.t.e.e.Example.methodWithArgs: < |
Argument and result¶
| Logging Level | Logging |
|---|---|
| TRACE, DEBUG |
DEBUG [] r.t.e.e.Example.methodWithArgs: > {data: {strParam: "s", numParam: "4"}} DEBUG [] r.t.e.e.Example.methodWithArgs: < {data: {out: "testResult"}} |
| INFO |
INFO [] r.t.e.e.Example.methodWithArgs: > INFO [] r.t.e.e.Example.methodWithArgs: < |
Selective logging¶
@Log.out
@Log.off
public String methodWithOnlyLogReturnAndArgs(String strParam,int numParam) {
return"testResult";
}
| Logging Level | Logging |
|---|---|
| TRACE, DEBUG |
INFO [] r.t.e.e.Example.methodWithArgs: < |
| INFO |
INFO [] r.t.e.e.Example.methodWithArgs: < |
MDC (Mapped Diagnostic Context)¶
The @Mdc annotation allows adding key-value pairs to MDC (Mapped Diagnostic Context) for structured logging.
MDC allows adding contextual information to each log message.
The annotation can be applied to methods and method parameters. Multiple application is supported.
Parameters of the @Mdc annotation:
key()- Key for the MDC entry. If not specified, the name of the annotated parameter is used.value()- Value for the MDC entry. If not specified, the value of the annotated parameter is used.global()- If true, the MDC value will be available globally within the thread, not just during method execution.
Parameter annotation¶
In this case, the MDC key will match the parameter name ("s"), and the value will be the parameter value.
Parameter annotation with key¶
Here, the MDC key will be "123", and the value will be the value of parameter "s".
Method use¶
This example demonstrates: - Method annotation with local MDC value
Combined¶
In this example, two MDC annotations are applied to the method, and one annotation is applied to the parameter.
Generated value for MDC value¶
When calling the method, an MDC entry will be added with the key "key" and a value as generated random UUID.
Example log with MDC:
INFO [main] r.t.e.e.Example.test: > {data: {s: "testValue"}} key=some-uuid-value key1=value2 123=testValue
Signatures¶
Available signatures for repository methods out of the box:
Class must be non final in order for aspects to work.
The T refers to the type of the return value, either Void.
T myMethod()Optional<T> myMethod()CompletionStage<T> myMethod()CompletionStageMono<T> myMethod()Project Reactor (require dependency)Flux<T> myMethod()Project Reactor (require dependency)
Class must be open in order for aspects to work.
By T we mean the type of the return value, either T?, either Unit.
myMethod(): Tsuspend myMethod(): TKotlin Coroutine (require dependency asimplementation)myMethod(): Flow<T>Kotlin Coroutine (require dependency asimplementation)