@pauloferreira25/aws-lambda-logger
v0.1.2
Published
Structured console.log logger for AWS Lambda, with AsyncLocalStorage-based correlation id propagation.
Downloads
243
Maintainers
Readme
@pauloferreira25/aws-lambda-logger
Structured, console.log-based logger built for AWS Lambda — no worker threads, no
transport machinery that doesn't survive a frozen execution environment. Correlation id
propagation via AsyncLocalStorage, so every log line inside a request carries it
automatically, with no need to thread it through every function call by hand.
Why not pino
pino's default transport (thread-stream) runs on a worker thread. That model fits a
long-running server; it doesn't fit Lambda, where the execution environment can be frozen
or torn down between invocations and CloudWatch already captures stdout directly. This
package writes one JSON line per log call, synchronously, with no background thread.
Install
npm install @pauloferreira25/aws-lambda-loggerUsage
import { makeLogger, runWithCorrelationId } from '@pauloferreira25/aws-lambda-logger'
const log = makeLogger({ config: { level: 'debug' } })
export const handler = (event, context) =>
runWithCorrelationId({
correlationId: context.awsRequestId,
run: async () => {
log.debug({ context: { event }, message: 'handler' })
// every log call inside this run — including ones made deep inside
// repository/service functions built at module load time — carries
// the same correlationId automatically.
},
})OpenTelemetry trace correlation
Every log line automatically includes traceId and spanId when an OpenTelemetry span is
active — no wiring required beyond having the application's own OTel setup in place (e.g. the
AWS Distro for OpenTelemetry Lambda layer, or a manually configured SDK). This package only
depends on @opentelemetry/api, the lightweight, side-effect-free API surface — it never
registers a global tracer or context manager itself. If the application hasn't configured
OpenTelemetry at all, traceId/spanId are simply omitted; nothing breaks.
correlationId and traceId/spanId serve different purposes and both can appear on the
same line: correlationId identifies one Lambda invocation, traceId identifies a trace that
may span multiple services.
API
makeLogger({ config: { level } })— returns aLogwithdebug,info,warn,error, each taking{ context, message }. Calls below the configured level are silently dropped.runWithCorrelationId({ correlationId, run })— runsrunwithcorrelationIdbound for its entire async duration, including anything it awaits transitively. Concurrent calls never leak into each other.getCorrelationId()— reads the correlationId bound by the nearest enclosingrunWithCorrelationId, orundefinedif none is active.getOtelTraceContext()— reads{ traceId, spanId }from the active OpenTelemetry span, orundefinedif none is active or OpenTelemetry isn't configured.
License
Not yet decided — add a LICENSE file and the license field in package.json before
publishing.
