@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
SettingautoLog: 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.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.
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 supportingimportance, subsystem, operation, track, security, metrics, userId,
service, appName, environment, and timestamps.
Configuration
Delivery
Logs are batched and flushed on a timer or oncebatchSize 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 thex-spl-signature and x-spl-timestamp headers.
maxAgeMs to change that.