New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@dotcom-reliability-kit/middleware-log-errors

Package Overview
Dependencies
Maintainers
4
Versions
62
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@dotcom-reliability-kit/middleware-log-errors

Express middleware to consistently log errors

  • 1.2.11
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
330
decreased by-36.42%
Maintainers
4
Weekly downloads
 
Created
Source

@dotcom-reliability-kit/middleware-log-errors

Express middleware to consistently log errors. This module is part of FT.com Reliability Kit.

Usage

Install @dotcom-reliability-kit/middleware-log-errors as a dependency:

npm install --save @dotcom-reliability-kit/middleware-log-errors

Include in your code:

import createErrorLogger from '@dotcom-reliability-kit/middleware-log-errors';
// or
const createErrorLogger = require('@dotcom-reliability-kit/middleware-log-errors');

createErrorLogger

The createErrorLogger function can be used to generate Express middleware which logs errors to the console and Splunk via n-logger.

Warning This middleware must be added to your Express app after all your application routes – you won't get error logs for any routes which are mounted after this middleware.

const app = express();
// App routes go here
app.use(createErrorLogger());

This will automatically serialize error objects and log them along with a serialized HTTP request which lead to the error being thrown. The information logged looks like this:

{
    event: 'HANDLED_ERROR',
    message: 'Error: something went wrong',

    error: {
        code: 'EXAMPLE_CODE',
        message: 'Something went wrong'
        // etc. (see `@dotcom-reliability-kit/serialize-error` linked above
        // for information about the logged properties
    },

    request: {
        id: 'abc123',
        method: 'GET',
        url: '/'
        // etc. (see `dotcom-reliability-kit/serialize-request` linked above
        // for information about the logged properties)
    },

    app: {
        commit: '137da65185397a7d699ed54c3052d10d83e82137',
        name: 'example-app',
        nodeVersion: '16.16.0',
        region: 'EU',
        releaseDate: '2022-07-25T01:37:00Z'
    }
}

Note If you're also using n-raven v6.1+ in your application then the Raven error logging will be deactivated so that you don't get double-logged errors.

Configuration options

Config options can be passed into the createErrorLogger function as an object with any of the keys below.

app.use(createErrorLogger({
    // Config options go here
}));
options.includeHeaders

An array of request headers to include in the serialized request object. This must be an Array of Strings, with each string being a header name. It's important that you do not include headers which include personally-identifiable-information, API keys, or other privileged information. This defaults to ['accept', 'content-type']. This option gets passed directly into dotcom-reliability-kit/serialize-request which has further documentation.

app.use(createErrorLogger({
    includeHeaders: [
        'accept',
        'content-length',
        'content-type',
        'user-agent'
    ]
}));

Note There's no need to include the x-request-id header in this array, as this is automatically included as request.id in the logs.

Contributing

See the central contributing guide for Reliability Kit.

License

Licensed under the MIT license.
Copyright © 2022, The Financial Times Ltd.

FAQs

Package last updated on 26 Oct 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc