@valbo/log-response-middleware

Express middleware that logs each response using a Winston logger.

Usage no npm install needed!

<script type="module">
  import valboLogResponseMiddleware from 'https://cdn.skypack.dev/@valbo/log-response-middleware';
</script>

README

@valbo/log-response-middleware

Express middleware that logs each response using a Winston logger.

npm (scoped) semantic-release Build Status Coverage Status Known Vulnerabilities

Install

npm install @valbo/log-response-middleware

Usage

Use it as soon as possible in the middleware chain. It will register a callback on the finish event of the response, so that when the response is sent it will log it with the provided logger.

import winston from 'winston';
import { createLogResponseMiddleware } from '@valbo/log-response-middleware';

app.use(createLogResponseMiddleware(winston));

Logs after the response is sent:

{"origin":"::1","user":"admin","request":{"method":"GET","url":"/login"},"response":{"status":200,"length":"226","ms":"105.558"}}

This package also exports a LogMessage interface which describes the format of the object that is logged, if you need it:

export interface LogMessage {
  origin: string;
  user: string; // from response.locals.user.username
  request: {
    method: string;
    url: string;
    body?: object;
  };
  response: {
    status: number;
    error?: {
      name: string;
      message: string;
    };
    length: unknown;
    ms: string;
  };
}