# @debugr/elastic 中文文档

规范地址：https://npmcn.com/package/%40debugr/elastic/

版本：4\.0\.0
npm 元数据更新时间：2026-08-13T21:36:15.896Z
中文译文更新时间：2026-11-11T18:54:14.615Z
软件包许可：MIT

本文由上游 README 翻译，可能存在偏差，请结合原文核对 API、命令与配置。版权与许可归原作者所有。

原始 npm 页面：https://www.npmjs.com/package/%40debugr/elastic
原文对照：https://npmcn.com/package/%40debugr/elastic/?lang=original
项目主页：https://github.com/cdn77/debugr
源码仓库：https://github.com/cdn77/debugr

## 关键信息（来自 npm 元数据）

- 包体积（解压后）：15.4 kB，13 个文件
- 模块格式：ESM + CJS
- TypeScript 类型：包内自带
- Node.js 要求：\>=24
- 运行依赖：1 个
- peer 依赖：@elastic/elasticsearch ^9
- 首次发布：2022-11-21T16:26:40.976Z
- 最新版本发布：2026-08-13T21:36:15.485Z
- 近 30 天下载量：9171（2026-10-12 至 2026-11-10，npm 官方下载统计）
- 已发布版本数：6（近 12 个月 1 个）

## 中文简介

调试器的弹性日志处理器

## 安装

```sh
npm install @debugr/elastic
```

也可以使用：`pnpm add @debugr/elastic`、`yarn add @debugr/elastic`、`bun add @debugr/elastic`

## README

# 调试器的弹性日志处理器

这个LogHandler通过SDK添加了Elastic的日志.

## 安装

```bash
npm install --save @debugr/elastic
```

## 使用量

```typescript
import { Logger, LogLevel } from '@debugr/core';
import { ElasticHandler } from '@debugr/elastic';

const globalContext = {
  applicationName: 'example',
};

const logger = new Logger(globalContext, [
  new ElasticHandler({
    index: 'example-app-logs',
  }),
]);

logger.setContextProperty('jobName', 'elasticTest');

logger.info('Application started.');
// should send a new entry to the `example-app-logs` index with the following content:
// {
//   level: 30,
//   message: 'Application started.',
//   ts: '2022-09-04T12:13:14Z',
//   context: {
//     applicationName: 'example',
//     jobName: 'elasticTest',
//   },
// }
```

### 选项

这个 `ElasticHandler` 构造器接受一个 _所需经费_ `options` 对象作为第一个参数. 一个实例 `Client` 类从 [`@elastic/elasticsearch`](https://www.npmjs.com/package/@elastic/elasticsearch) SDK可以当作第二个 参数; 如果不提供, 构造者将尝试使用所提供的 `options` 对象。 所以当用一个单一的论据 给建筑师打电话时 `options` 对象是一个结合 在此定义的日志处理器选项和弹性 `Client` 选项。 见 [`@elastic/elasticsearch`](https://www.npmjs.com/package/@elastic/elasticsearch) 可用软件包文档 `Client` 选项。 处理器本身的现有选项 内容如下:

| 选项 | 类型 | 默认 | 说明 |
| --- | --- | --- | --- |
| `index` | `string`, (中文(简体) ), `(entry: LogEntry) => string` | _(必须)_ | 发送条目的弹性索引 。 您可以指定一个回调来将每个条目路径到一个基于不同索引,例如基于关卡的索引. |
| `threshold` | `LogLevel` | `LogLevel.ALL` | 将登录的条目最低水平 。 此关卡以下的任何条目都将被忽略 . |
| `bodyMapper` | `(entry: LogEntry) => Record<string, any>` | _(见说明)_ | 调用转换a `LogEntry` 输入要发送给 Elastic 的数据对象。 除 `globalContext` 财务报告和审定财务报表 `taskContext` 将合并为单个对象 `context` 对象,以及 `data` 对象将被序列化为 JSON . |
| `errorCallback` | `(error: Error) => void` | _(见说明)_ | 向 Elastic 发送一个条目时会调用的回调失败 。 默认的回调会简单地将错误记录入控制台. |
| `errorMsThreshold` | `number` | _(无)_ | 自上次通知以来的最少时间 `errorCallback` 然后再打电话. |

## 常见问题

### @debugr/elastic 是什么？

调试器的弹性日志处理器

### 如何安装 @debugr/elastic？

在项目目录运行 npm install @debugr/elastic，或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖：@elastic/elasticsearch（^9）。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖，Yarn 需要手动安装。

### @debugr/elastic 支持 TypeScript 吗？

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

### @debugr/elastic 支持 ESM 还是 CommonJS？

同时提供 ES Module 与 CommonJS 入口，import 和 require\(\) 都可以使用，打包工具会自动选择合适的版本。

### @debugr/elastic 需要什么版本的 Node\.js？

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

### @debugr/elastic 可以商用吗？

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

### @debugr/elastic 的下载量有多少？

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

### @debugr/elastic 还在维护吗？

最新版本 4\.0\.0 发布于 2026/08/14（2 个月前）；至今共发布 6 个版本，其中过去 12 个月发布了 1 个；首次发布于 2022/11/22。发布频率只是参考信号：功能稳定的工具可能很少需要更新，建议结合源码仓库的 issue 和提交记录判断。

