-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathFileAuditLogFlushScheduler.java
More file actions
45 lines (41 loc) · 1.73 KB
/
Copy pathFileAuditLogFlushScheduler.java
File metadata and controls
45 lines (41 loc) · 1.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package com.digitalsanctuary.spring.user.audit;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
/**
* Scheduled task that periodically flushes the audit log buffer to disk.
*
* <p>This component ensures buffered audit data is written to the file at regular intervals,
* balancing write performance with data integrity.
*
* <p><strong>Conditional Activation:</strong> This scheduler is only active when both conditions are met:
* <ul>
* <li>{@code user.audit.logEvents=true} - audit logging is enabled</li>
* <li>{@code user.audit.flushOnWrite=false} - immediate flush is disabled</li>
* </ul>
*
* <p>When flush-on-write is enabled, logs are flushed immediately after each write,
* making this scheduler unnecessary. The flush frequency is controlled by
* {@code user.audit.flushRate} (in milliseconds).
*
* @see FileAuditLogWriter
* @see AuditConfig
*/
@Slf4j
@Component
@ConditionalOnExpression("${user.audit.logEvents:true} && !${user.audit.flushOnWrite:true}")
@RequiredArgsConstructor
public class FileAuditLogFlushScheduler {
private final FileAuditLogWriter fileAuditLogWriter;
/**
* Flushes the audit log buffer to the file. This method is called on a schedule to ensure that the buffer is flushed periodically to balance
* performance with data integrity.
*/
@Scheduled(fixedRateString = "#{@auditConfig.flushRate}")
public void flushAuditLog() {
log.info("FileAuditLogFlushScheduler.flushAuditLog: Flushing audit log buffer to file.");
fileAuditLogWriter.flushWriter();
}
}