Koa-bunyan-logger is a Koa middleware designed for integrating structured logging into Koa v2.x applications, leveraging the popular Bunyan logging library. The current stable version is 2.1.0. While its release cadence has been infrequent, updates typically coincide with significant Koa version shifts or critical bug fixes. This library aims to be flexible and lightweight, allowing applications to customize logging behavior without excessive overhead. A key differentiator is its ability to provide flexible log context, request logging, and explicit support for ignoring specific paths, ensuring that only relevant requests generate log entries. It allows passing an existing Bunyan logger instance or creating a new one with options, and handles request IDs for tracing. It provides middleware specifically for setting request context and logging request/response cycles, including duration and status-based log levels. This package is built on CommonJS but can be imported into ESM projects via interop.
npm install koa-bunyan-loggerVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates setting up a Koa v2 application with `koa-bunyan-logger`. It shows how to initialize the logger, apply request ID context and request/response logging middleware (including path ignoring), use `ctx.log` within custom middleware, and properly configure Koa's error handling for structured logs. The code uses modern ESM syntax.
Ensure your Koa application is running version 2.x or later. If you must use Koa v1.x, downgrade `koa-bunyan-logger` to a v1.x release.
Review and update any custom log field accesses to use properties available on the native Node.js `req` and `res` objects.
Add `app.on('error', () => {});` to your Koa application's initialization code to suppress the default error logging.Adopt the pattern `import koaBunyanLogger from 'koa-bunyan-logger';` followed by `app.use(koaBunyanLogger.requestLogger());` instead of attempting named imports like `import { requestLogger } from 'koa-bunyan-logger';`.Ensure Koa is initialized correctly for v2.x: change `const app = koa();` to `const app = new Koa();`
Run your Node.js application and pipe its output to the `bunyan` command-line tool. For example: `node your_app.js | npx bunyan -o short`.