Koa Logger Middleware
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.
Common errors
-
TypeError: app.use is not a function
cause Koa is not correctly initialized or imported, or 'app' is not a Koa application instance.fixEnsure 'koa' is installed (`npm install koa`) and you are creating a Koa application instance with `const Koa = require('koa'); const app = new Koa();`. -
TypeError: koa_logger_1.default is not a function
cause Attempting to use an ESM default import syntax (`import logger from 'koa-logger';`) for a package that is primarily CommonJS in an environment where direct CJS-ESM interop is not configured or fails.fixUse the CommonJS require syntax: `const logger = require('koa-logger');`. If you must use ESM, ensure your build setup correctly handles CJS module imports. -
ReferenceError: logger is not defined
cause The `koa-logger` middleware was not imported or assigned to the `logger` variable before being used.fixAdd `const logger = require('koa-logger');` at the top of your file to import the middleware.
Warnings
- breaking Koa-logger v4.0.0 dropped support for Node.js versions older than 10. Users on Node.js < 18 (the recommended version for v4) should upgrade their Node.js runtime or use an older version of koa-logger.
- breaking Koa-logger v4.0.0 explicitly adds support for Koa v3. While it might work with Koa v2, compatibility issues may arise due to potential API changes in Koa itself. It's recommended to align Koa and koa-logger versions.
- gotcha For comprehensive logging, koa-logger should be applied as early as possible in the middleware stack (e.g., `app.use(logger())` near the top). Placing it after other middleware might result in those preceding middleware's operations not being fully captured in the log.
Install
-
npm install koa-logger -
yarn add koa-logger -
pnpm add koa-logger
Imports
- logger
import logger from 'koa-logger'
const logger = require('koa-logger') - logger
import { logger } from 'koa-logger';import logger from 'koa-logger';
Quickstart
const Koa = require('koa');
const logger = require('koa-logger');
const app = new Koa();
// Use koa-logger middleware as early as possible to wrap all subsequent middleware
app.use(logger());
// Example middleware to handle requests
app.use(async (ctx, next) => {
if (ctx.path === '/') {
ctx.body = 'Hello, Koa-logger!';
} else if (ctx.path === '/users') {
ctx.body = [{ id: 1, name: 'Alice' }, { id: 2, name: 'Bob' }];
} else {
ctx.status = 404;
ctx.body = 'Not Found';
}
await next(); // Pass control to subsequent middleware
});
const port = process.env.PORT || 3000;
app.listen(port, () => {
console.log(`Koa server running on http://localhost:${port}`);
});
// To run this:
// 1. npm install koa koa-logger
// 2. node your-file-name.js
// 3. Open http://localhost:3000 or http://localhost:3000/users in your browser.