Skip to main content

Audit logging library with pluggable backends

Project description

airlog

codecov

A Python audit logging library with pluggable backends and compliance-grade features.

Features

Feature Details
Immutable log stream AuditStream is append-only – no modify or delete operations
Structured events AuditEvent is a frozen dataclass with nanosecond-precision timestamps
Correlation IDs correlation_id field tracks a request across its entire lifecycle
Principal tracking Principal captures subject, auth method (password / API key / certificate / JWT / …), and arbitrary metadata
Integrity verification SHA-256 checksum over all event fields; event.verify() detects tampering
Unique event IDs UUID v4 per event + monotonically increasing sequence numbers
Pluggable backends Implement AuditStream.emit() to target stdout, files, syslog, databases, or any custom destination
loguru / JSON LoguruAuditStream ships out of the box for newline-delimited JSON output
Thread-safe Sequence counter is protected by a lock

Requirements

  • Python 3.11+
  • uv (recommended package manager)

Installation

uv add airlog

Or with pip:

pip install airlog

Quick start

from airlog import LoguruAuditStream, Principal

stream = LoguruAuditStream()          # JSON → stderr by default

event = stream.record(
    "login",
    principal=Principal(subject="alice", auth_method="password", metadata={"ip": "10.0.0.1"}),
    resource="session",
    resource_id="ses-42",
    correlation_id="req-abc123",
)

# Verify integrity at any time
assert event.verify()
print(event.event_id)     # UUID v4
print(event.sequence)     # 1
print(event.timestamp)    # UTC datetime
print(event.checksum)     # SHA-256 hex

Custom backend

from airlog import AuditEvent, AuditStream, Principal

class SyslogStream(AuditStream):
    def emit(self, event: AuditEvent) -> None:
        import syslog
        syslog.syslog(f"[{event.sequence}] {event.action} by {event.principal.subject}")

stream = SyslogStream()
stream.record(
    "delete",
    principal=Principal(subject="bob", auth_method="certificate"),
    resource="document",
    resource_id="doc-7",
    outcome="success",
)

Sample JSON output (LoguruAuditStream)

Each call to record() emits one newline-delimited JSON line. All audit fields are stored under record.extra:

{
  "record": {
    "extra": {
      "event_id": "4e1b2f3c-…",
      "sequence": 1,
      "timestamp_ns": 1735000000000000000,
      "timestamp": "2025-01-01T00:00:00+00:00",
      "action": "login",
      "principal_subject": "alice",
      "principal_auth_method": "password",
      "principal_metadata": {"ip": "10.0.0.1"},
      "resource": "session",
      "resource_id": "ses-42",
      "outcome": "success",
      "correlation_id": "req-abc123",
      "context": {},
      "checksum": "a3f1…"
    }
  }
}

Examples

Comprehensive examples are available in the examples/ directory:

  • Basic Usage: Simple getting started examples with various adapters
  • Pipeline & Middleware: Building pipelines with enrichment and redaction
  • Context Tracking: Automatic event enrichment using audit contexts
  • Metrics & Monitoring: Collecting metrics from audit events
  • Policy & Routing: Policy-based event routing to different streams
  • Integrity & Verification: Tamper detection and integrity checking
  • Retention Management: Configuring retention rules and automatic cleanup
  • Backend Adapters: OCSF format and OpenTelemetry integration
  • Registry: Using the global pipeline registry

Run any example:

python examples/basic_usage.py
# or
uv run examples/basic_usage.py

See examples/README.md for the complete list and details.

Development

make install     # install all dependencies

make lint        # check formatting + lint rules
make format      # auto-format and fix lint issues

make test        # run tests with coverage (≥90 %)
make build       # build wheel/sdist + twine check
make release     # test + build

make clean       # remove dist/, coverage output

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

airlog-1.0.0.tar.gz (101.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

airlog-1.0.0-py3-none-any.whl (62.3 kB view details)

Uploaded Python 3

File details

Details for the file airlog-1.0.0.tar.gz.

File metadata

  • Download URL: airlog-1.0.0.tar.gz
  • Upload date:
  • Size: 101.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for airlog-1.0.0.tar.gz
Algorithm Hash digest
SHA256 7b1fcfea1975735ec5dc48c62336c99a45e6abaf88206f33c0c0f3aa2e7ba382
MD5 cdd586baa61acf69e4fccded064bebee
BLAKE2b-256 da7aecda21921e9bcdf9b6d88cfc03224d74030fbdf7e69af290abc6d6581d09

See more details on using hashes here.

Provenance

The following attestation bundles were made for airlog-1.0.0.tar.gz:

Publisher: publish.yml on ashforge-rs/airlog

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file airlog-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: airlog-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 62.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for airlog-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 3e2c35acadc7fb0e3a784b4b8354542a9e7e84b106d0b28e23496efa3ec4030f
MD5 2e36584843edeabd8399542bb736e284
BLAKE2b-256 09d97258822c89f1345e4f3495ab5cafb3d978657fff9399bca94f77370f83ab

See more details on using hashes here.

Provenance

The following attestation bundles were made for airlog-1.0.0-py3-none-any.whl:

Publisher: publish.yml on ashforge-rs/airlog

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page