@valbo/log-response-middleware
TypeScript icon, indicating that this package has built-in type declarations

1.0.4 • Public • Published

@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;
  };
}

Package Sidebar

Install

npm i @valbo/log-response-middleware

Weekly Downloads

5

Version

1.0.4

License

MIT

Unpacked Size

20.4 kB

Total Files

24

Last publish

Collaborators

  • valbo