Koa-logger is a development-style logging middleware for Koa.js applications, providing colorful, concise HTTP request and response logging to the console. The current stable version, 4.0.0, was released after a period of lower activity, signaling a revival of the project and bringing support for Koa v3 and Node.js versions 18 and higher. It outputs request method, URL, response status, duration, and content length, often formatted with ANSI colors for readability. A key differentiator is its customizable `transporter` option, which allows developers to redirect log output to alternative pipes or logging systems, rather than just `process.stdout`. While primarily a CommonJS package, its updates ensure compatibility with modern Node.js environments and Koa framework versions.
npm install koa-loggerVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to initialize a basic Koa application and integrate `koa-logger` as the primary logging middleware, showing its effect on console output for various HTTP requests.
Upgrade Node.js to version 18 or higher. If unable to upgrade Node.js, consider using koa-logger v3.x or earlier.
Ensure your Koa application is running Koa v3.x when using koa-logger v4.x. If using Koa v2.x, consider pinning koa-logger to an earlier major version (e.g., v3.x).
Always place `app.use(logger())` as one of the first middleware in your Koa application setup to ensure it 'wraps' all subsequent middleware and accurately logs all request/response cycles.
Ensure 'koa' is installed (`npm install koa`) and you are creating a Koa application instance with `const Koa = require('koa'); const app = new Koa();`.Use the CommonJS require syntax: `const logger = require('koa-logger');`. If you must use ESM, ensure your build setup correctly handles CJS module imports.Add `const logger = require('koa-logger');` at the top of your file to import the middleware.