{"id":17447,"library":"koa-pino-logger","title":"Koa Pino Logger","description":"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.","status":"active","version":"5.0.0","language":"javascript","source_language":"en","source_url":"https://github.com/pinojs/koa-pino-logger","tags":["javascript","koa","http","logger","fast","pino"],"install":[{"cmd":"npm install koa-pino-logger","lang":"bash","label":"npm"},{"cmd":"yarn add koa-pino-logger","lang":"bash","label":"yarn"},{"cmd":"pnpm add koa-pino-logger","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Runtime dependency as it's a Koa middleware. koa-pino-logger v5 is compatible with Koa v2+.","package":"koa","optional":false},{"reason":"Core logging library used by the middleware. Users are expected to have pino installed, especially for CLI tools like `pino-pretty`.","package":"pino","optional":false}],"imports":[{"note":"While the example uses `require()`, modern Koa applications often use ESM. The default export is the middleware function.","wrong":"const koaPinoLogger = require('koa-pino-logger').default","symbol":"koaPinoLogger","correct":"import koaPinoLogger from 'koa-pino-logger'"},{"note":"CommonJS `require('koa')` is still prevalent in older projects, but `import Koa from 'koa'` is standard for ESM.","wrong":"const Koa = require('koa')","symbol":"Koa","correct":"import Koa from 'koa'"},{"note":"Types for Koa's middleware are generally imported from 'koa' or '@types/koa'. koa-pino-logger extends `ctx.log`.","wrong":"import { KoaMiddleware } from 'koa'","symbol":"Middleware","correct":"import { Middleware } from 'koa'"}],"quickstart":{"code":"import Koa from 'koa';\nimport pinoLogger from 'koa-pino-logger';\n\nconst app = new Koa();\n\n// Initialize the logger middleware\nconst loggerMiddleware = pinoLogger();\napp.use(loggerMiddleware);\n\n// Access the logger via ctx.log within downstream middleware\napp.use((ctx) => {\n  ctx.log.info({ user: 'example-user' }, 'Request processed for user');\n  ctx.body = 'Hello world with logging';\n});\n\nconst PORT = process.env.PORT ?? 3000;\napp.listen(PORT, () => {\n  console.log(`Server listening on port ${PORT}`);\n  console.log('Test with: curl http://localhost:3000 | pino');\n});","lang":"javascript","description":"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."},"warnings":[{"fix":"Upgrade your Node.js runtime to version 18 or higher.","message":"Version 5.0.0 drops support for Node.js 12 and adds support for Node.js 18. Ensure your environment meets this new requirement.","severity":"breaking","affected_versions":">=5.0.0"},{"fix":"Review Pino v7 release notes and adapt any custom Pino configurations, especially around transports and exit handling.","message":"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()`.","severity":"breaking","affected_versions":">=4.0.0"},{"fix":"Ensure your Koa application is running on Koa v2 or later. If on Koa v1, refer to the `v1 readme` on the package's GitHub or migrate your Koa application.","message":"koa-pino-logger is designed for Koa v2 and above. Using it with Koa v1 (which uses generator functions) will lead to compatibility issues.","severity":"gotcha","affected_versions":">=2.0.0"},{"fix":"Add `app.silent = true;` after `const app = new Koa();` to suppress Koa's default error logging.","message":"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.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-22T00:00:00.000Z","next_check":"2026-07-21T00:00:00.000Z","problems":[{"fix":"Ensure `app.use(logger());` is called early in your Koa application's middleware chain, before any middleware that attempts to use `ctx.log`.","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.","error":"TypeError: ctx.log is undefined"},{"fix":"Install Koa: `npm install koa` or `yarn add koa`.","cause":"The `koa` package, a peer dependency, is not installed in the project.","error":"Error: Cannot find module 'koa'"},{"fix":"Install Pino: `npm install pino` or `yarn add pino`. Ensure it's available for `koa-pino-logger` to use.","cause":"The `pino` package, a peer dependency, is not installed, or `koa-pino-logger`'s internal dependency resolution for `pino` failed.","error":"Error: Cannot find module 'pino'"},{"fix":"Verify 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');`).","cause":"Attempting to use `app.use` on an object that is not a Koa application instance, or `Koa` was imported incorrectly.","error":"TypeError: app.use is not a function"}],"ecosystem":"npm","meta_description":null}