Skip to main content
The AuditRails Java SDK is a zero-dependency library that uses only HttpURLConnection from the standard library. It targets Java 11+ and is designed for production use: a background thread drains the event buffer, exponential-backoff retries handle transient failures, and AutoCloseable ensures safe shutdown in Spring Boot applications and try-with-resources blocks alike.

Installation

Add the dependency to your build file. The artifact is published to Maven Central.

Initialization

Use the fluent builder to create an AuditRails instance. The API key is the only required argument.
AuditRails implements AutoCloseable, so you can use it in a try-with-resources block for short-lived contexts (such as scripts or tests). In long-running applications, prefer Spring Boot’s destroyMethod or a JVM shutdown hook — see Graceful Shutdown.

Configuration options

Logging events

audit.log() adds an AuditEvent to the in-memory buffer and returns immediately. The buffer is flushed in the background on the flushIntervalMs schedule. This method never throws.

Direct (immediate) logging

audit.logDirect() sends the event immediately and returns a LogResponse. It throws AuditRailsException on failure.

Direct batch logging

Send multiple events in a single HTTP request, bypassing the buffer. Returns a BatchResponse with all assigned log IDs.

Manual flush

Framework integration

Spring Boot

Declare AuditRails as a Spring-managed singleton bean. Setting destroyMethod = "close" ensures Spring calls audit.close() during application shutdown, flushing all buffered events before the JVM exits.
# application.yml

Spring MVC interceptor

Use a HandlerInterceptor to automatically audit every API request. afterCompletion fires after the response has been committed, so you always capture the final HTTP status.
Register the interceptor in your WebMvcConfigurer:

Error handling

log() never throws. For logDirect() and logBatchDirect(), catch AuditRailsException to inspect the failure.

Graceful shutdown

AuditRails implements java.lang.AutoCloseable. Calling close() flushes all buffered events and stops the background flush thread. Spring Boot (recommended): Use @Bean(destroyMethod = "close") as shown above. Spring will call close() automatically when the application context is shut down. Try-with-resources: For short-lived use cases (scripts, tests, CLI tools):
Manual shutdown hook: If you are not using Spring, register a JVM shutdown hook:
Calling close() more than once is safe — subsequent calls are no-ops. Do not call log() after close() has been called; events submitted after shutdown are silently discarded.