pub struct AuditLogger<R: AuditEventRepository> { /* private fields */ }Expand description
AuditLogger service for simplified audit event recording
This service provides a convenient API for recording audit events from application code and middleware. It handles:
- Automatic context extraction from HTTP requests
- Actor detection from authentication context
- Async, non-blocking logging
- Error handling (audit failures are logged but don’t break requests)
§Example
ⓘ
let audit_logger = AuditLogger::new(audit_repo);
audit_logger.log(
tenant_id,
AuditAction::EventIngested,
Actor::api_key("key-123".to_string(), "prod-api-key".to_string()),
)
.with_resource("event_stream".to_string(), "stream-456".to_string())
.with_context(request_context)
.record_async()
.await;Implementations§
Source§impl<R: AuditEventRepository> AuditLogger<R>
impl<R: AuditEventRepository> AuditLogger<R>
Sourcepub fn log(
&self,
tenant_id: TenantId,
action: AuditAction,
actor: Actor,
) -> AuditLogEntry<'_, R>
pub fn log( &self, tenant_id: TenantId, action: AuditAction, actor: Actor, ) -> AuditLogEntry<'_, R>
Start building an audit log entry
Sourcepub async fn log_success(
&self,
tenant_id: TenantId,
action: AuditAction,
actor: Actor,
) -> Result<(), AllSourceError>
pub async fn log_success( &self, tenant_id: TenantId, action: AuditAction, actor: Actor, ) -> Result<(), AllSourceError>
Log a successful event (convenience method)
Sourcepub async fn log_failure(
&self,
tenant_id: TenantId,
action: AuditAction,
actor: Actor,
error_message: String,
) -> Result<(), AllSourceError>
pub async fn log_failure( &self, tenant_id: TenantId, action: AuditAction, actor: Actor, error_message: String, ) -> Result<(), AllSourceError>
Log a failed event (convenience method)
Sourcepub async fn log_resource_action(
&self,
tenant_id: TenantId,
action: AuditAction,
actor: Actor,
resource_type: String,
resource_id: String,
outcome: AuditOutcome,
) -> Result<(), AllSourceError>
pub async fn log_resource_action( &self, tenant_id: TenantId, action: AuditAction, actor: Actor, resource_type: String, resource_id: String, outcome: AuditOutcome, ) -> Result<(), AllSourceError>
Log an event with resource information (convenience method)
Sourcepub async fn record_silently(&self, event: AuditEvent)
pub async fn record_silently(&self, event: AuditEvent)
Record an event without returning an error (logs errors internally) This is useful for middleware where audit failures shouldn’t break requests
Sourcepub async fn log_batch(
&self,
events: Vec<AuditEvent>,
) -> Result<(), AllSourceError>
pub async fn log_batch( &self, events: Vec<AuditEvent>, ) -> Result<(), AllSourceError>
Batch log multiple events
Sourcepub async fn log_batch_silently(&self, events: Vec<AuditEvent>)
pub async fn log_batch_silently(&self, events: Vec<AuditEvent>)
Batch log multiple events without returning an error
Auto Trait Implementations§
impl<R> Freeze for AuditLogger<R>
impl<R> RefUnwindSafe for AuditLogger<R>where
R: RefUnwindSafe,
impl<R> Send for AuditLogger<R>
impl<R> Sync for AuditLogger<R>
impl<R> Unpin for AuditLogger<R>
impl<R> UnwindSafe for AuditLogger<R>where
R: RefUnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more