koa-logger

Development-friendly HTTP request logger middleware that prints method, URL, status, response time, and body size to stdout.

Since Koa 2 Spec ↗

Syntax

import logger from 'koa-logger';
app.use(logger());

Parameters

NameTypeRequiredDescription
transporter function No Optional custom log function `(str, args) => void`. Defaults to `console.log`. Useful for integrating with winston, pino, etc.

Returns

function — Koa middleware that logs each request/response pair.

Examples

import Koa from 'koa';
import logger from 'koa-logger';

const app = new Koa();

app.use(logger());

app.use(async (ctx) => {
  ctx.body = { ok: true };
});

app.listen(3000);
Output
<-- GET /
--> GET / 200 4ms 10b
// Custom transporter to integrate with pino
import pino from 'pino';
const log = pino();

app.use(logger((str, args) => {
  log.info({ args }, str.trim());
}));
Output
{"level":30,"msg":"  --> GET / 200 3ms 10b","args":[...]}

Notes

Use `koa-logger` in development for human-readable output. In production, replace it with a structured logger such as `pino` or `winston` and the custom `transporter` option. Mount it as the first middleware so it wraps all subsequent layers and measures total response time accurately.

See also