@crowlog/async-context-plugin

中文文档
2.1.1发布于 2026/01/02MITTypeScriptES Module零依赖构建来源已验证近 30 天下载 2498

Request-scoped logging context plugin for Crowlog using Node.js AsyncLocalStorage. Perfect for tracking request IDs and user context across async operations.

原始使用文档
文档目录6 节
  1. Installation
  2. Usage
  3. Examples
  4. Hono JS
  5. License
  6. Credits

Crowlog - Async Context Plugin

This package is a plugin for the Crowlog logger. It allows to simply add context data to the logs using node async context. It's useful in http servers to add log context that follow the lifecycle of a request (like a request id or user id).

[!IMPORTANT] This plugin is only available environment that supports async_hooks like Node.js or cloudflare workers with compatibility flags.

Installation

// pnpm 
pnpm install @crowlog/logger @crowlog/async-context-plugin

// npm
npm install @crowlog/logger @crowlog/async-context-plugin

// yarn
yarn add @crowlog/logger @crowlog/async-context-plugin

Usage

Use the plugin by first creating a logger with the plugin:

import { createLogger } from '@crowlog/logger';
import { createAsyncContextPlugin, addLogContext, wrapWithLoggerContext } from '@crowlog/async-context-plugin';

const logger = createLogger({ namespace: 'my-app', plugins: [createAsyncContextPlugin()] });

// create a context, some initial context data can be added
wrapWithLoggerContext({ requestId: '123' }, () => {
  addLogContext({ userId: '123' });

  // logs within the context will have the requestId and userId
  logger.info('Hello world');
});

Examples

Hono JS

import { createMiddleware } from 'hono';
import { addLogContext, wrapWithLoggerContext } from '@crowlog/async-context-plugin';

const loggerMiddleware = createMiddleware(async (context, next) => {
  const requestId = getHeader({ context, name: 'x-request-id' }) ?? generateId();

  await wrapWithLoggerContext({ requestId }, next);
});

const authMiddleware = createMiddleware(async (context, next) => {
  const userId = /* get the user id from your auth layer */;

  addLogContext({ userId });

  await next();
});
import { Hono } from 'hono';
import { createLogger } from '@crowlog/logger';
import { createAsyncContextPlugin } from '@crowlog/async-context-plugin';

const app = new Hono();
const logger = createLogger({ namespace: 'my-app', plugins: [createAsyncContextPlugin()] });

app.use(loggerMiddleware);
app.use(authMiddleware);

app.get('/', (c) => {
  // logs will have the requestId and userId
  logger.info('Request received');

  return c.json({ message: 'Hello World' });
});

License

This project is licensed under the MIT License. See the LICENSE file for more information.

Credits

This project is crafted with ❤️ by Corentin Thomasset. If you find this project helpful, please consider supporting my work.

版本与发布记录

@crowlog/async-context-plugin 共发布 10 个版本,首次发布于 2025/02/07;过去 12 个月发布了 2 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
2.1.1latest2026/01/0210 个月前
2.1.02025/12/0111 个月前
2.0.02025/10/011.1 年前
1.2.12025/04/211.6 年前
1.2.02025/04/211.6 年前
1.1.12025/02/101.7 年前

关于 @crowlog/async-context-plugin 的常见问题

@crowlog/async-context-plugin 是什么?

Request-scoped logging context plugin for Crowlog using Node.js AsyncLocalStorage. Perfect for tracking request IDs and user context across async operations.

如何安装 @crowlog/async-context-plugin?

在项目目录运行 npm install @crowlog/async-context-plugin,或使用 pnpm add / yarn add / bun add。

@crowlog/async-context-plugin 支持 TypeScript 吗?

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

@crowlog/async-context-plugin 支持 ESM 还是 CommonJS?

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

@crowlog/async-context-plugin 需要什么版本的 Node.js?

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

@crowlog/async-context-plugin 可以商用吗?

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

@crowlog/async-context-plugin 的下载量有多少?

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

@crowlog/async-context-plugin 还在维护吗?

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