Skip to content

The only tool you need to know what is happening and how to fix it.

692 B
Raw Blame
import winston from 'winston';

const logger = winston.createLogger({ level: 'info', transports: [new winston.transports.Console()] });

// One line per request ("GET /api/products/4182 200 14ms"), inside the request's trace.
export function accessLog(req, res, next) {
  const start = process.hrtime.bigint();
  res.on('finish', () => {
    const ms = Number((process.hrtime.bigint() - start) / 1000000n);
    logger.info(`${req.method} ${req.path} ${res.statusCode} ${ms}ms`, {
      'http.request.method': req.method,
      'url.path': req.path,
      'http.route': req.route?.path ?? '',
      'http.response.status_code': res.statusCode,
      duration_ms: ms,
    });
  });
  next();
}