1.0.4 • Published 3 years ago

@valbo/log-response-middleware v1.0.4

Weekly downloads
89
License
MIT
Repository
github
Last release
3 years ago

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

3 years ago

1.0.3

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago