@axiomhq/logging

中文文档
0.4.1发布于 2026/09/09MITTypeScriptESM + CJS零依赖构建来源已验证近 30 天下载 83.9万

The official logging package for Axiom

原始使用文档
文档目录6 节
  1. Proxy transport delivery
  2. Schema validation
  3. Requirements
  4. Install
  5. Documentation
  6. License

Axiom Logging library

The @axiomhq/logging package allows you to send structured logs to Axiom from any JavaScript application.

// lib/axiom/logger.ts
import axiomClient from '@/lib/axiom/axiom';
import { Logger, ConsoleTransport, AxiomJSTransport } from '@axiomhq/logging';

export const logger = new Logger({
  transports: [
    new AxiomJSTransport({
      axiom: axiomClient,
      dataset: process.env.AXIOM_DATASET!,
      axiomClient: 'my-app/1.0',
    }),
    new ConsoleTransport({ prettyPrint: true }),
  ],
});

logger.info('Hello World!');

AxiomJSTransport appends logging package usage to the Axiom client's X-Axiom-Client header. With the example above, requests use an X-Axiom-Client header like axiom-js/<version> axiom-logging/<version> my-app/1.0.

Proxy transport delivery

ProxyTransport uses SimpleFetchTransport to send logs to your HTTP endpoint. Delivery is best effort:

  • The transport retains at most 1,000 queued events plus one batch of at most 1,000 events in flight. When the queue fills, it drops the oldest queued events to retain recent activity. The in-flight batch is unchanged. These limits bound event counts, not bytes or individual event sizes.
  • With autoFlush: true, the first queued event starts a two-second timer. Later events do not postpone it, and a full queue starts a send immediately when no batch is in flight. { durationMs } sets a different delay. With autoFlush omitted or false, sending starts only when you call flush().
  • Requests run one at a time. Network failures and HTTP 429, 500, 502, 503, and 504 receive at most one retry after a one-second delay. A longer Retry-After is honored. Both attempts and their delays share a ten-second deadline.
  • Permanent failures, caller cancellation, timeout, or an exhausted retry drop the batch. If Retry-After cannot fit within the deadline, the batch is dropped without retrying early. Subsequent batches also respect that server cooldown and are dropped if they cannot wait within their own deadline.
  • Drops produce console.warn diagnostics containing counts by reason, without log contents. Warnings are emitted at most once every 30 seconds per transport; suppressed counts are included in the next warning when more drops occur.

flush() waits for events accepted before that call to be sent or dropped. Later events do not extend that caller's target, and concurrent calls share the serial sender. A resolved flush does not guarantee delivery. Retrying after a lost response can duplicate events that the server already accepted.

Earlier versions retained failed batches for a later flush. The bounded retry policy above replaces that behavior, so a prolonged outage can cause data loss without allowing the backlog to grow indefinitely.

Schema validation

Logger can validate and type log fields using any validator that implements Standard Schema v1 (for example zod).

import { z } from 'zod';
import { Logger, ConsoleTransport } from '@axiomhq/logging';

const LogFieldsSchema = z
  .object({
    userId: z.string(),
    action: z.enum(['login', 'logout']),
  })
  .strict();

const logger = new Logger({
  transports: [new ConsoleTransport()],
  schema: LogFieldsSchema,
  onValidationError: (context) => {
    console.warn('Dropped invalid log', context.stage, context.reason, context.issues);
  },
});

logger.info('User action', { userId: '123', action: 'login' });

Invalid logs are dropped before transport delivery. schema validates input fields before formatters run, and outputSchema can validate the final formatted event:

const OutputSchema = z.object({
  eventName: z.string(),
  userId: z.string(),
});

const logger = new Logger({
  transports: [new ConsoleTransport()],
  schema: LogFieldsSchema,
  outputSchema: OutputSchema,
  formatters: [
    (event) => ({
      eventName: event.message,
      userId: event.fields.userId,
    }),
  ],
});

Unknown key behavior depends on your schema configuration:

  • strict object schemas (like z.object(...).strict()) reject unknown keys
  • non-strict schemas may allow them

logger.raw(...) bypasses schema validation.

Requirements

Node.js 20 or higher is required. Node.js 18 is no longer supported.

Install

npm install @axiomhq/js @axiomhq/logging

Documentation

For more information about how to set up and use the @axiomhq/logging package, see the axiom.co/docs/guides/javascript.

License

MIT

版本与发布记录

@axiomhq/logging 共发布 15 个版本,首次发布于 2025/02/20;过去 12 个月发布了 8 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
0.4.1latest2026/09/091 个月前
0.4.02026/08/043 个月前
0.3.02026/06/294 个月前
0.2.22026/05/125 个月前
0.2.12026/05/076 个月前
0.2.02026/01/279 个月前

关于 @axiomhq/logging 的常见问题

@axiomhq/logging 是什么?

The official logging package for Axiom

如何安装 @axiomhq/logging?

在项目目录运行 npm install @axiomhq/logging,或使用 pnpm add / yarn add / bun add。

@axiomhq/logging 支持 TypeScript 吗?

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

@axiomhq/logging 支持 ESM 还是 CommonJS?

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

@axiomhq/logging 需要什么版本的 Node.js?

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

@axiomhq/logging 可以商用吗?

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

@axiomhq/logging 的下载量有多少?

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

@axiomhq/logging 还在维护吗?

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