![require(esm) Backported to Node.js 20, Paving the Way for ESM-Only Packages](https://cdn.sanity.io/images/cgdhsj6q/production/be8ab80c8efa5907bc341c6fefe9aa20d239d890-1600x1097.png?w=400&fit=max&auto=format)
Security News
require(esm) Backported to Node.js 20, Paving the Way for ESM-Only Packages
require(esm) backported to Node.js 20, easing the transition to ESM-only packages and reducing complexity for developers as Node 18 nears end-of-life.
@dotcom-reliability-kit/middleware-log-errors
Advanced tools
Express middleware to consistently log errors
Express middleware to consistently log errors. This module is part of FT.com Reliability Kit.
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. It must be added to your Express app after all your application routes:
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: {
// See `@dotcom-reliability-kit/serialize-error` (linked above)
// for information about the logged properties
},
request: {
// 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.
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 String
s, 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'
]
}));
See the central contributing guide for Reliability Kit.
Licensed under the MIT license.
Copyright © 2022, The Financial Times Ltd.
FAQs
Express middleware to consistently log errors
The npm package @dotcom-reliability-kit/middleware-log-errors receives a total of 496 weekly downloads. As such, @dotcom-reliability-kit/middleware-log-errors popularity was classified as not popular.
We found that @dotcom-reliability-kit/middleware-log-errors demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
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.
Security News
require(esm) backported to Node.js 20, easing the transition to ESM-only packages and reducing complexity for developers as Node 18 nears end-of-life.
Security News
PyPI now supports iOS and Android wheels, making it easier for Python developers to distribute mobile packages.
Security News
Create React App is officially deprecated due to React 19 issues and lack of maintenance—developers should switch to Vite or other modern alternatives.