Skip to main content

AuditLogger

Struct AuditLogger 

Source
pub struct AuditLogger { /* private fields */ }
Expand description

The main audit logger, dispatching events to configured sinks.

Thread-safe and designed for high-throughput operation. Sinks can be added at any time, even after wrapping in Arc.

§Retention Policy

AuditLogger itself does not enforce a retention policy — it forwards events to sinks and forgets them. Retention is the responsibility of each sink:

  • MemorySink: bounded by max_capacity (FIFO eviction).
  • WriterSink: appends forever; pair with external log rotation (e.g. logrotate, cloud blob lifecycle rules).
  • TracingSink: inherits the tracing subscriber’s retention.

For production deployments, configure a log rotation / archival pipeline and monitor the chronix_audit_events_total counter to ensure events are not silently dropped.

Implementations§

Source§

impl AuditLogger

Source

pub fn new() -> Self

Create a new audit logger with no sinks.

Source

pub fn with_hmac_key(self, key: Vec<u8>) -> Self

Create an audit logger with an HMAC key.

When set, each event is sealed with HMAC-SHA-256 instead of plain SHA-256. An attacker with database write access cannot recompute the hash chain without possessing the key.

Source

pub fn resume_from(self, last_id: u64, last_hash: Option<String>) -> Self

Continue an existing chain instead of starting a new one.

A restart used to reset the sequence to 1 and the previous hash to None, so every restart began a fresh chain in the same file. A verifier then could not tell a restart from a truncation: both look like an event whose prev_hash is None in the middle of the log. Anchoring on the last event already on disk makes the file one chain across the whole life of the deployment.

Source

pub fn is_keyed(&self) -> bool

Whether the chain is sealed with a key rather than a bare hash.

Source

pub fn add_sink(&self, sink: Box<dyn AuditSink>)

Add a sink. Can be called at any time, including after wrapping in Arc.

Source

pub fn validate_has_durable_sink(&self) -> Result<()>

**** Validate that at least one durable audit sink is configured.

Returns Ok(()) if at least one sink reports is_durable() == true. Returns an error if no durable sinks are present, which means audit events would be lost on process restart — a compliance violation in regulated environments.

Call this at startup after configuring all sinks.

Source

pub fn has_durable_sink(&self) -> bool

Check if at least one durable sink is configured.

Source

pub fn log(&self, event: AuditEvent)

Log an audit event to all sinks.

Assigns a monotonic sequence number, seals the event with a SHA-256 hash chain, and updates metrics.

Source

pub fn next_sequence(&self) -> u64

Current sequence number (next ID to be assigned).

Source

pub fn sink_count(&self) -> usize

Number of sinks.

Source

pub fn flush(&self)

Flush all sinks.

Source

pub fn log_strict(&self, event: AuditEvent) -> Result<()>

Strict audit logging — fail-closed mode for regulated environments.

Unlike log, this attempts all sinks even when some fail, and returns a PartialDelivery error if any sink could not emit. This gives the caller full visibility into which sinks succeeded vs failed so it can make an informed decision on whether to abort the protected operation.

Source

pub fn flush_strict(&self) -> Result<()>

Strict flush — tries all sinks, reports partial failures.

Use this together with log_strict for fail-closed audit contexts where all buffered events must be durably persisted before acknowledging the protected operation.

Trait Implementations§

Source§

impl Default for AuditLogger

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

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
Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more