Skip to main content
@snaplog/express sends structured, batched events from an Express app. Events are buffered in memory and delivered in the background, so your responses stay fast.

Installation

express ^4 or ^5 is required as a peer dependency.

Quick start

1. Create a logger

2. Attach the middleware

3. Log anywhere

Automatic request logging

Setting autoLog: true logs every incoming request with no extra code. Each request produces an audit event carrying:
The fields the middleware generates are camelCase, as shown above. Any track data you pass yourself uses snake_case, matching the other SDKs.
Change which header supplies the request id:
Requests that abort mid-flight, such as a client disconnect, are logged as error events instead. Leave autoLog off — the default is false — to log nothing automatically and call the logger yourself.

Error handling

snapLogErrorHandler captures thrown errors with their stack traces. Register it after all your routes.
Errors are logged as error events with the message and stack in track.error and track.stack. Turn the stack off with:
The handler calls next(err) after logging, so your existing error handling is unaffected.

Logging

Metadata

Every manual method takes a message and an optional metadata object supporting importance, subsystem, operation, track, security, metrics, userId, service, appName, environment, and timestamps.
The NestJS-style methods take a context string instead:

Configuration

Delivery

Logs are batched and flushed on a timer or once batchSize is reached. A failed send is retried with exponential backoff, and only retryable responses are retried: 408, 429, and any 5xx. A batch that exhausts its retries is dropped with a console error. Each request has a 10 second timeout. The in-memory queue holds up to 10,000 logs; when full, the oldest is dropped. Flush before you exit if you are not relying on the process exit hooks:

Verify webhooks

Verify SnapLog alert webhooks before processing them. The signature and timestamp come from the x-spl-signature and x-spl-timestamp headers.
HMAC signing uses the raw request body, so you must enable the rawBody capture shown above or verification will fail.
For lower-level use:
Webhooks older than 60 seconds are rejected. Pass maxAgeMs to change that.

Exports