@dotcom-reliability-kit/log-error

中文文档
6.0.3发布于 2026/11/11MITTypeScriptES Module近 30 天下载 4176

A method to consistently log error object with optional request information

原始使用文档
文档目录8 节
  1. Usage
  2. logHandledError
  3. logRecoverableError
  4. logUnhandledError
  5. Configuration options
  6. Migrating
  7. Contributing
  8. License

@dotcom-reliability-kit/log-error

A method to consistently log error object with optional request information. This module is part of FT.com Reliability Kit.

Usage

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

npm install --save @dotcom-reliability-kit/log-error

Include in your code:

import {logRecoverableError} from '@dotcom-reliability-kit/log-error';

[!TIP] If you're using this package with TypeScript, we recommend using the following settings in your tsconfig.json file to avoid type errors:

{
    "esModuleInterop": true,
    "module": "nodenext",
    "moduleResolution": "nodenext"
}

logHandledError

The logHandledError function can be used to log errors consistently to the console and Splunk via Reliability Kit logger. This method is used to indicate that the error being logged has been correctly handled and the application can continue to run.

logHandledError({
    error: new Error('Something went wrong')
});

This will automatically serialize error objects and log them. 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
    },

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

logRecoverableError

The logRecoverableError function can be used to log errors consistently to the console and Splunk via Reliability Kit logger. This method is used to indicate that the error being logged was completely recoverable, with no error page sent to a user.

logRecoverableError({
    error: new Error('Something went wrong')
});

The information logged looks like this:

{
    event: 'RECOVERABLE_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
    },

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

logUnhandledError

The logUnhandledError function can be used to log errors consistently to the console and Splunk via Reliability Kit logger. This method is used to indicate that the error being logged was not recoverable and resulted in an application crashing.

logUnhandledError({
    error: new Error('Something went wrong')
});

The information logged looks like this:

{
    event: 'UNHANDLED_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
    },

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

Configuration options

Config options can be passed into all of the provided logging functions as an object, with the keys below:

logRecoverableError({
    // Options go here
});

options.error

The error object to log. This is the only required option.

logRecoverableError({
    error: new Error('Something went wrong')
});

options.includeHeaders

An array of request headers to include in the serialized request object (if one is provided with options.request). 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 option gets passed directly into dotcom-reliability-kit/serialize-request which has further documentation.

This option defaults to:

[
    'accept',
    'accept-encoding',
    'accept-language',
    'content-type',
    'referer',
    'user-agent'
]

Example of usage:

logRecoverableError({
    // ...other required options
    includeHeaders: [
        'accept',
        'content-length',
        'content-type',
        'user-agent'
    ]
});

The default set of headers is also available to use, so that you don't need to repeat them if you want to add new included headers. You'll need to import @dotcom-reliability-kit/serialize-request, then these headers are available:

import { DEFAULT_INCLUDED_HEADERS } from '@dotcom-reliability-kit/serialize-request';

logRecoverableError({
    // ...other required options
    includeHeaders: [
        ...DEFAULT_INCLUDED_HEADERS,
        'my-custom-header'
    ]
});

[!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.

options.logger

A logger object which implements two methods: error and warn. It may implement other methods but they're not used. The methods have a very permissive signature:

type LogMethod = (...logData: any) => any;

Though it's best if they can accept a single object and output results as JSON.

This option defaults to Reliability Kit logger.

options.logUserErrorsAsWarnings

[!NOTE]
This option is only available in the logHandledError function.

A boolean indicating whether to log user errors (those with a 400–499 status property) with a level of warn rather than error. This helps to reduce the amount of error-level logs that you need to focus on.

This option defaults to false.

options.request

A request object (e.g. an instance of Express.Request or an object with method and url properties) to include alongside the error in the log. This will be automatically serialized with @dotcom-reliability-kit/serialize-request.

app.get('/example', (request, response, next) => {
    logRecoverableError({
        // ...other required options
        request: request
    });
    next();
});

When this option is defined, the logged data looks includes request data:

{
    event: 'RECOVERABLE_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: '22.13.0',
        region: 'EU',
        releaseDate: '2022-07-25T01:37:00Z'
    }
}

Migrating

Consult the Migration Guide if you're trying to migrate to a later major version of this package.

Contributing

See the central contributing guide for Reliability Kit.

License

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

版本与发布记录

@dotcom-reliability-kit/log-error 共发布 74 个版本,首次发布于 2022/06/13;过去 12 个月发布了 8 个版本。

版本发布日期距今
6.0.3latest2026/11/112 天前
6.0.22026/08/202 个月前
6.0.12026/07/144 个月前
6.0.02026/06/234 个月前
6.0.0-beta.1prerelease2026/06/035 个月前
5.1.82026/03/128 个月前

关于 @dotcom-reliability-kit/log-error 的常见问题

@dotcom-reliability-kit/log-error 是什么?

A method to consistently log error object with optional request information

如何安装 @dotcom-reliability-kit/log-error?

在项目目录运行 npm install @dotcom-reliability-kit/log-error,或使用 pnpm add / yarn add / bun add。

@dotcom-reliability-kit/log-error 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@dotcom-reliability-kit/log-error 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@dotcom-reliability-kit/log-error 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js ^22.12 || ^24 || ^26。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@dotcom-reliability-kit/log-error 可以商用吗?

@dotcom-reliability-kit/log-error 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@dotcom-reliability-kit/log-error 的下载量有多少?

根据 npm 官方下载统计,@dotcom-reliability-kit/log-error 在 2026/10/13 – 2026/11/11 这 30 天内被下载 4,176 次,日均约 139 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@dotcom-reliability-kit/log-error 还在维护吗?

最新版本 6.0.3 发布于 2026/11/11(2 天前);至今共发布 74 个版本,其中过去 12 个月发布了 8 个;首次发布于 2022/06/13。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。