{"id":17107,"library":"koa-logger","title":"Koa Logger Middleware","description":"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.","status":"active","version":"4.0.0","language":"javascript","source_language":"en","source_url":"https://github.com/koajs/logger","tags":["javascript","koa","middleware","logger","log"],"install":[{"cmd":"npm install koa-logger","lang":"bash","label":"npm"},{"cmd":"yarn add koa-logger","lang":"bash","label":"yarn"},{"cmd":"pnpm add koa-logger","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Koa-logger is a middleware for Koa.js applications and requires Koa to function. It is an implied peer dependency.","package":"koa","optional":false}],"imports":[{"note":"Koa-logger v4.x is primarily a CommonJS package. While Node.js 18+ supports ESM, direct 'import' statements for this package may not work as expected without specific transpilation or Node.js configurations. Use 'require' for direct CommonJS usage.","wrong":"import logger from 'koa-logger'","symbol":"logger","correct":"const logger = require('koa-logger')"},{"note":"While primarily CommonJS, if you are using an environment that transpiles CommonJS to ESM or are using Node.js's CJS-ESM interop, `import logger from 'koa-logger'` would be the correct pattern for its default export. `import { logger } from 'koa-logger'` is incorrect as it's not a named export.","wrong":"import { logger } from 'koa-logger';","symbol":"logger","correct":"import logger from 'koa-logger';"}],"quickstart":{"code":"const Koa = require('koa');\nconst logger = require('koa-logger');\n\nconst app = new Koa();\n\n// Use koa-logger middleware as early as possible to wrap all subsequent middleware\napp.use(logger());\n\n// Example middleware to handle requests\napp.use(async (ctx, next) => {\n  if (ctx.path === '/') {\n    ctx.body = 'Hello, Koa-logger!';\n  } else if (ctx.path === '/users') {\n    ctx.body = [{ id: 1, name: 'Alice' }, { id: 2, name: 'Bob' }];\n  } else {\n    ctx.status = 404;\n    ctx.body = 'Not Found';\n  }\n  await next(); // Pass control to subsequent middleware\n});\n\nconst port = process.env.PORT || 3000;\napp.listen(port, () => {\n  console.log(`Koa server running on http://localhost:${port}`);\n});\n\n// To run this:\n// 1. npm install koa koa-logger\n// 2. node your-file-name.js\n// 3. Open http://localhost:3000 or http://localhost:3000/users in your browser.","lang":"javascript","description":"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."},"warnings":[{"fix":"Upgrade Node.js to version 18 or higher. If unable to upgrade Node.js, consider using koa-logger v3.x or earlier.","message":"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.","severity":"breaking","affected_versions":">=4.0.0"},{"fix":"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).","message":"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.","severity":"breaking","affected_versions":">=4.0.0"},{"fix":"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.","message":"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.","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 'koa' is installed (`npm install koa`) and you are creating a Koa application instance with `const Koa = require('koa'); const app = new Koa();`.","cause":"Koa is not correctly initialized or imported, or 'app' is not a Koa application instance.","error":"TypeError: app.use is not a function"},{"fix":"Use the CommonJS require syntax: `const logger = require('koa-logger');`. If you must use ESM, ensure your build setup correctly handles CJS module imports.","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.","error":"TypeError: koa_logger_1.default is not a function"},{"fix":"Add `const logger = require('koa-logger');` at the top of your file to import the middleware.","cause":"The `koa-logger` middleware was not imported or assigned to the `logger` variable before being used.","error":"ReferenceError: logger is not defined"}],"ecosystem":"npm","meta_description":null}