Skip to content

Commit 1789a4b

Browse files
committed
feat(audit): enhance audit messages with enriched context and implement AuditableDTO
1 parent ceb4bfd commit 1789a4b

3 files changed

Lines changed: 28 additions & 4 deletions

File tree

backend/src/main/java/com/park/utmstack/aop/logging/impl/AuditAspect.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,22 +49,26 @@ private Object handleAudit(ProceedingJoinPoint joinPoint,
4949
extra.put("layer", ApplicationLayer.CONTROLLER.getValue());
5050

5151
try {
52+
attemptMessage = enrichMessage(attemptMessage, extra);
5253
applicationEventService.createEvent(attemptMessage, attemptType, extra);
5354

5455
Object result = joinPoint.proceed();
5556

5657
if (successType != ApplicationEventType.UNDEFINED) {
58+
successMessage = enrichMessage(successMessage, extra);
5759
applicationEventService.createEvent(successMessage, successType, extra);
5860
}
5961

6062
return result;
6163

6264
} catch (Exception e) {
65+
String msg = String.format("%s: %s", context, e.getMessage());
6366
if (!e.getClass().isAnnotationPresent(NoLogException.class)) {
64-
String msg = String.format("%s: %s", context, e.getMessage());
6567
log.error(msg, e, StructuredArguments.keyValue("args", logContextBuilder.buildArgs(e)));
6668
}
6769

70+
applicationEventService.createEvent(msg, ApplicationEventType.ERROR);
71+
6872
throw e;
6973
}
7074
}
@@ -78,5 +82,18 @@ private Map<String, Object> extractAuditData(Object[] args) {
7882
}
7983
return extra;
8084
}
85+
86+
private String enrichMessage(String message, Map<String, Object> values) {
87+
if (message == null || !message.contains("{")) {
88+
return message;
89+
}
90+
91+
String enriched = message;
92+
for (Map.Entry<String, Object> entry : values.entrySet()) {
93+
enriched = enriched.replace("{" + entry.getKey() + "}", String.valueOf(entry.getValue()));
94+
}
95+
96+
return enriched;
97+
}
8198
}
8299

backend/src/main/java/com/park/utmstack/service/dto/application_modules/GroupConfigurationDTO.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,24 @@
22

33
import com.park.utmstack.domain.application_modules.UtmModuleGroupConfiguration;
44
import com.park.utmstack.domain.application_modules.validators.ValidModuleConfiguration;
5+
import com.park.utmstack.service.dto.auditable.AuditableDTO;
56
import lombok.Data;
67

78
import javax.validation.constraints.NotEmpty;
89
import javax.validation.constraints.NotNull;
910
import java.util.List;
11+
import java.util.Map;
1012

1113
@Data
1214
@ValidModuleConfiguration
13-
public class GroupConfigurationDTO {
15+
public class GroupConfigurationDTO implements AuditableDTO {
1416
@NotNull
1517
private Long moduleId;
1618
@NotEmpty
1719
private List<UtmModuleGroupConfiguration> keys;
20+
21+
@Override
22+
public Map<String, Object> toAuditMap() {
23+
return Map.of("moduleId", moduleId);
24+
}
1825
}

backend/src/main/java/com/park/utmstack/web/rest/application_modules/UtmModuleGroupConfigurationResource.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@ public class UtmModuleGroupConfigurationResource {
3838
@PutMapping("/module-group-configurations/update")
3939
@AuditEvent(
4040
attemptType = ApplicationEventType.CONFIG_UPDATE_ATTEMPT,
41-
attemptMessage = "Attempt to update configuration keys initiated",
41+
attemptMessage = "Attempt to update configuration keys initiated for moduleId={moduleId}",
4242
successType = ApplicationEventType.CONFIG_UPDATE_SUCCESS,
43-
successMessage = "Configuration keys updated successfully"
43+
successMessage = "Configuration keys updated successfully for moduleId={moduleId}"
4444
)
4545
public ResponseEntity<Void> updateConfiguration(@Valid @RequestBody GroupConfigurationDTO body) {
4646
final String ctx = CLASSNAME + ".updateConfiguration";

0 commit comments

Comments
 (0)