@elastic/ecs-helpers

中文文档
2.1.1发布于 2023/11/01Apache-2.0CommonJS零依赖近 30 天下载 297.8万

ecs-logging-nodejs helpers

原始使用文档
文档目录7 节
  1. Install
  2. API
  3. version
  4. formatError(obj, err) -> bool
  5. formatHttpRequest(obj, req) -> bool
  6. formatHttpResponse(obj, res)
  7. License

@elastic/ecs-helpers

Build Status js-standard-style

A set of helpers for the other ecs-logging-nodejs packages. You should not directly use this package.

Install

npm install @elastic/ecs-helpers

API

version

The currently supported version of Elastic Common Schema.

formatError(obj, err) -> bool

A function that adds ECS Error fields for a given Error object. It returns true iff the given err was an Error object it could process.

const { formatError } = require('@elastic/ecs-helpers')
const logRecord = { msg: 'oops', /* ... */ }
formatError(logRecord, new Error('boom'))
console.log(logRecord)

will show:

{
  msg: 'oops',
  error: {
    type: 'Error',
    message: 'boom',
    stack_trace: 'Error: boom\n' +
      '    at REPL30:1:26\n' +
      '    at Script.runInThisContext (vm.js:133:18)\n' +
      // ...
  }
}

The ECS logging libraries typically use this to automatically handle an err metadata field passed to a logging statement. E.g. log.warn({err: myErr}, '...') for pino, log.warn('...', {err: myErr}) for winston.

formatHttpRequest(obj, req) -> bool

Function that enhances an ECS object with http request data. The given request object, req, must be one of the following:

The function returns true iff the given req was a request object it could process. Note that currently this notably does not process a http.ClientRequest as returned from http.request().

const http = require('http')
const { formatHttpRequest } = require('@elastic/ecs-helpers')

http.createServer(function (req, res) {
  res.end('hi')

  const obj = {}
  formatHttpRequest(obj, req)
  console.log('obj:', JSON.stringify(obj, null, 4))
}).listen(3000)

Running this and making a request via curl http://localhost:3000/ will print something close to:

obj: {
    "http": {
        "version": "1.1",
        "request": {
            "method": "get",
            "headers": {
                "host": "localhost:3000",
                "accept": "*/*"
            }
        }
    },
    "url": {
        "full": "http://localhost:3000/",
        "path": "/"
    },
    "client": {
        "address": "::1",
        "ip": "::1",
        "port": 61969
    },
    "user_agent": {
        "original": "curl/7.64.1"
    }
}

formatHttpResponse(obj, res)

Function that enhances an ECS object with http response data. The given request object, req, must be one of the following:

The function returns true iff the given res was a response object it could process. Note that currently this notably does not process a http.IncomingMessage that is the argument to the "response" event of a client http.request()

const http = require('http')
const { formatHttpRequest } = require('@elastic/ecs-helpers')

http.createServer(function (req, res) {
  res.setHeader('Foo', 'Bar')
  res.end('hi')

  const obj = {}
  formatHttpResponse(obj, res)
  console.log('obj:', JSON.stringify(obj, null, 4))
}).listen(3000)

Running this and making a request via curl http://localhost:3000/ will print something close to:

rec: {
    "http": {
        "response": {
            "status_code": 200,
            "headers": {
                "foo": "Bar"
            }
        }
    }
}

License

This software is licensed under the Apache 2 license.

版本与发布记录

@elastic/ecs-helpers 共发布 12 个版本,首次发布于 2020/02/11;过去 12 个月发布了 0 个版本。

版本发布日期距今
2.1.1latest2023/11/013 年前
2.1.02023/10/313 年前
2.0.02023/10/263 年前
1.1.02021/03/195 年前
1.0.02021/02/095 年前
0.6.02021/01/265 年前

关于 @elastic/ecs-helpers 的常见问题

@elastic/ecs-helpers 是什么?

ecs-logging-nodejs helpers

如何安装 @elastic/ecs-helpers?

在项目目录运行 npm install @elastic/ecs-helpers,或使用 pnpm add / yarn add / bun add。

@elastic/ecs-helpers 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/elastic__ecs-helpers 是否存在。

@elastic/ecs-helpers 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@elastic/ecs-helpers 需要什么版本的 Node.js?

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

@elastic/ecs-helpers 可以商用吗?

@elastic/ecs-helpers 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@elastic/ecs-helpers 的下载量有多少?

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

@elastic/ecs-helpers 还在维护吗?

最新版本 2.1.1 发布于 2023/11/01(3 年前);至今共发布 12 个版本,其中过去 12 个月发布了 0 个;首次发布于 2020/02/11。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。