Koa Pino Logger
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
-
TypeError: ctx.log is undefined
cause The `koa-pino-logger` middleware was not applied to the Koa application, or `ctx.log` is being accessed before the middleware has executed for a given request.fixEnsure `app.use(logger());` is called early in your Koa application's middleware chain, before any middleware that attempts to use `ctx.log`. -
Error: Cannot find module 'koa'
cause The `koa` package, a peer dependency, is not installed in the project.fixInstall Koa: `npm install koa` or `yarn add koa`. -
Error: Cannot find module 'pino'
cause The `pino` package, a peer dependency, is not installed, or `koa-pino-logger`'s internal dependency resolution for `pino` failed.fixInstall Pino: `npm install pino` or `yarn add pino`. Ensure it's available for `koa-pino-logger` to use. -
TypeError: app.use is not a function
cause Attempting to use `app.use` on an object that is not a Koa application instance, or `Koa` was imported incorrectly.fixVerify that `app` is an instance of `Koa` (e.g., `const app = new Koa();`) and that `koa` is correctly imported (e.g., `import Koa from 'koa';` or `const Koa = require('koa');`).
Warnings
- breaking Version 5.0.0 drops support for Node.js 12 and adds support for Node.js 18. Ensure your environment meets this new requirement.
- breaking Version 4.0.0 updated to use `pino@7`. This may introduce breaking changes or deprecations inherited from Pino itself, particularly concerning how transports work with `pino.transport` and handling of `pino.final()`.
- gotcha koa-pino-logger is designed for Koa v2 and above. Using it with Koa v1 (which uses generator functions) will lead to compatibility issues.
- gotcha When logging thrown errors, Koa's default error handling prints to `console.error`. To prevent duplicate output with `koa-pino-logger`, set `app.silent = true` on your Koa application instance.
Install
-
npm install koa-pino-logger -
yarn add koa-pino-logger -
pnpm add koa-pino-logger
Imports
- koaPinoLogger
const koaPinoLogger = require('koa-pino-logger').defaultimport koaPinoLogger from 'koa-pino-logger'
- Koa
const Koa = require('koa')import Koa from 'koa'
- Middleware
import { KoaMiddleware } from 'koa'import { Middleware } from 'koa'
Quickstart
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');
});