Merge pull request #2336 from bosch-io/feature/audit-log-wildcard

Audit log wildcard * introduction to include all parameters by default
This commit is contained in:
Denislav Prinov
2025-04-01 10:26:37 +03:00
committed by GitHub
2 changed files with 18 additions and 6 deletions

View File

@@ -34,7 +34,7 @@ public @interface AuditLog {
String message() default ""; String message() default "";
String[] logParams() default {}; String[] logParams() default {"*"};
boolean logResponse() default false; boolean logResponse() default false;
} }

View File

@@ -30,8 +30,16 @@ import org.springframework.stereotype.Component;
public class AuditLoggingAspect { public class AuditLoggingAspect {
/** /**
* Around advice that applies to methods annotated with @AuditLog. * Provides around advice for methods annotated with {@code @AuditLog}.
* It logs the request and, if logResponse is true, the response as well. * <p>
* By default, all method parameters are logged. To restrict logging to specific parameters,
* specify them via the {@code logParams} attribute (e.g., {@code = {"param1", "param2"}}).
* To disable parameter logging, use an empty array (i.e., {@code logParams = {}}).
* </p>
* <p>
* This advice logs the request details and, if {@code logResponse} is set to {@code true},
* logs the method response as well.
* </p>
*/ */
@Around("@annotation(auditLog)") @Around("@annotation(auditLog)")
public Object handleAuditLogging(final ProceedingJoinPoint joinPoint, final AuditLog auditLog) throws Throwable { public Object handleAuditLogging(final ProceedingJoinPoint joinPoint, final AuditLog auditLog) throws Throwable {
@@ -86,9 +94,9 @@ public class AuditLoggingAspect {
private String getParamsToLog(final JoinPoint joinPoint, final AuditLog auditLog) { private String getParamsToLog(final JoinPoint joinPoint, final AuditLog auditLog) {
final Object[] args = joinPoint.getArgs(); final Object[] args = joinPoint.getArgs();
final String[] includeParams = auditLog.logParams(); final String[] logParams = auditLog.logParams();
if (includeParams.length == 0) { if (isLogAll(logParams)) {
return Arrays.deepToString(args); return Arrays.deepToString(args);
} else { } else {
final MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature(); final MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
@@ -97,7 +105,7 @@ public class AuditLoggingAspect {
.boxed() .boxed()
.collect(Collectors.toMap(i -> paramNames[i], i -> args[i])); .collect(Collectors.toMap(i -> paramNames[i], i -> args[i]));
return Arrays.stream(includeParams) return Arrays.stream(logParams)
.filter(paramMap::containsKey) .filter(paramMap::containsKey)
.map(name -> name + "=" + paramMap.get(name)) .map(name -> name + "=" + paramMap.get(name))
.collect(Collectors.joining(", ")); .collect(Collectors.joining(", "));
@@ -130,6 +138,10 @@ public class AuditLoggingAspect {
return resultMessageBuilder.build(); return resultMessageBuilder.build();
} }
private boolean isLogAll(String [] logParams) {
return Arrays.asList(logParams).contains("*");
}
@Builder @Builder
private record ResultMessage(String message, AuditLog.Level level) {} private record ResultMessage(String message, AuditLog.Level level) {}
} }