A minimal Next.js example showing how to integrate LogLayer with instrumentation.ts and route server-side console.* calls through a shared logger.
This repository is the companion example for my article:
Logging in Next.js with LogLayer: Instrumentation, Console Override, and Structured Logs
- Shared LogLayer instance
- Server-side
console.*interception withinstrumentation.ts - Structured errors and metadata
- Environment-based transports
- Direct LogLayer usage when structured context is needed
npm install
npm run devOpen http://localhost:3000 and check the terminal output.
src/instrumentation.ts— hooks into Next.js startup and overrides server-sideconsolemethodssrc/lib/logger/index.ts— creates and configures the shared LogLayer instancesrc/lib/logger/utils/console.ts— mapsconsole.*calls to LogLayer and handles errors and metadata
Existing code can keep using console:
console.log("User created", { userId: "123" });
console.error(new Error("Something went wrong"));On the server, those calls are routed through LogLayer and emitted as structured logs.
For code that needs richer context, LogLayer can also be used directly.
This example intentionally keeps the setup small and does not include dd-trace or other observability integrations.
The goal is to show the core logging setup before adding platform-specific concerns such as Datadog or OpenTelemetry.
For the reasoning behind the setup, runtime differences, console interception, and structured logging approach: