Koa Pino Logger

5.0.0 · active · verified Wed Apr 22

koa-pino-logger is a high-performance Koa middleware for integrating the Pino JSON logger into web applications. Currently at version 5.0.0, it offers rapid, structured logging with minimal overhead, differentiating itself from alternatives like koa-bunyan-logger, koa-logger, and koa-morgan through its speed, native JSON output, support for arbitrary data, and avoidance of `eval` for performance. Its release cadence is sporadic, typically aligning with major updates to the underlying `pino` logger or the `Koa` framework. It's particularly favored for production environments where log processing efficiency and structured output are critical for observability platforms.

Common errors

Warnings

Install

Imports

Quickstart

This example demonstrates how to set up `koa-pino-logger` in a basic Koa application to automatically log requests and allows accessing the logger instance on `ctx.log` for custom logging within middleware.

import Koa from 'koa';
import pinoLogger from 'koa-pino-logger';

const app = new Koa();

// Initialize the logger middleware
const loggerMiddleware = pinoLogger();
app.use(loggerMiddleware);

// Access the logger via ctx.log within downstream middleware
app.use((ctx) => {
  ctx.log.info({ user: 'example-user' }, 'Request processed for user');
  ctx.body = 'Hello world with logging';
});

const PORT = process.env.PORT ?? 3000;
app.listen(PORT, () => {
  console.log(`Server listening on port ${PORT}`);
  console.log('Test with: curl http://localhost:3000 | pino');
});

view raw JSON →