@contrast/logger

中文文档
1.44.1发布于 2026/11/05SEE LICENSE IN LICENSETypeScriptCommonJS近 30 天下载 1.7万

Centralized logging for Contrast agent services

原始使用文档
文档目录7 节
  1. Usage
  2. Structured Logging
  3. Filtering
  4. Best Practices
  5. Errors
  6. Objects
  7. Namespacing

@contrast/logger

Usage

Centralized, structured logging for Contrast agent services, built on Pino. @contrast/agentify composes the logger onto core, so components use core.logger:

core.logger.info({ foo, bar }, 'message with %s, %d', 'string', 123);

Outside the agent, install it onto your own core object:

const { default: install } = require('@contrast/logger');

const logger = install(core); // also sets core.logger

See Pino documentation for the logger's API.

Structured Logging

Pino logs as JSON, so it is particularly good at logging JavaScript objects.

Given:

logger.info('hello world');
logger.error('this is at error level');
logger.info('the answer is %d', 42);
logger.info({ obj: 42 }, 'hello world');
logger.info({ obj: 42, b: 2 }, 'hello world');
logger.info({ nested: { obj: 42 } }, 'nested');

Log output looks like the following:

{"level":30,"time":1649266923912,"pid":12345,"hostname":"foo","msg":"hello world"}
{"level":50,"time":1649266923913,"pid":12345,"hostname":"foo","msg":"this is at error level"}
{"level":30,"time":1649266923913,"pid":12345,"hostname":"foo","msg":"the answer is 42"}
{"level":30,"time":1649266923913,"pid":12345,"hostname":"foo","obj":42,"msg":"hello world"}
{"level":30,"time":1649266923913,"pid":12345,"hostname":"foo","obj":42,"b":2,"msg":"hello world"}
{"level":30,"time":1649266923913,"pid":12345,"hostname":"foo","nested":{"obj":42},"msg":"nested"}

You can use pino-pretty to format logs during development. The previous lines, when passed to pino-pretty, will format to the following:

node example.js | npx pino-pretty
[1649266998216] INFO (12345 on foo): hello world
[1649266998216] ERROR (12345 on foo): this is at error level
[1649266998216] INFO (12345 on foo): the answer is 42
[1649266998216] INFO (12345 on foo): hello world
    obj: 42
[1649266998216] INFO (12345 on foo): hello world
    obj: 42
    b: 2
[1649266998216] INFO (12345 on foo): nested
    nested: {
      "obj": 42
    }

Filtering

In addition to being easier to read when pretty-printed, JSON logging also allows us to filter with utilities like jq.

For example, we can filter only error messages:

node example.js | jq "select(.level == 50)"
Output (click to expand)
{
  "level": 50,
  "time": 1649268122322,
  "pid": 12345,
  "hostname": "foo",
  "msg": "this is at error level"
}

Or, we can look up only log entries with the obj property:

node example.js | jq "select(.obj)"
Output (click to expand)
{
  "level": 30,
  "time": 1649268235040,
  "pid": 12345,
  "hostname": "foo",
  "obj": 42,
  "msg": "hello world"
}
{
  "level": 30,
  "time": 1649268235040,
  "pid": 12345,
  "hostname": "foo",
  "obj": 42,
  "b": 2,
  "msg": "hello world"
}

See jq's tutorial and manual for examples on just how powerful JSON processing can be!

Best Practices

Errors

Pino handles errors specially when they are passed as err or when passed as the first argument. See: https://github.com/pinojs/pino/blob/master/docs/api.md#serializers-object

For example, when handling an error we should use the following pattern(s):

try {
  // ...something that throws
} catch (err) {
  logger.warn(err);
  // if we want to customize the message:
  logger.warn(err, 'something bad happened');
  // or if we need to provide additional data:
  logger.warn({ err, data }, 'something broke when handling %s', filename);
}
Output (click to expand)
[1649264653814] WARN (12345 on foo): yikes!
  err: {
    "type": "Error",
    "message": "yikes!",
    "stack":
        Error: yikes!
            at Object.<anonymous> (/path/to/file.js:5:13)
            at Module._compile (internal/modules/cjs/loader.js:1085:14)
            at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10)
            at Module.load (internal/modules/cjs/loader.js:950:32)
            at Function.Module._load (internal/modules/cjs/loader.js:790:12)
            at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:75:12)
            at internal/main/run_main_module.js:17:47
  }

[1649264653814] WARN (12345 on foo): something bad happened
  err: {
    "type": "Error",
    "message": "yikes!",
    "stack":
        Error: yikes!
            at Object.<anonymous> (/path/to/file.js:5:13)
            at Module._compile (internal/modules/cjs/loader.js:1085:14)
            at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10)
            at Module.load (internal/modules/cjs/loader.js:950:32)
            at Function.Module._load (internal/modules/cjs/loader.js:790:12)
            at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:75:12)
            at internal/main/run_main_module.js:17:47
  }

[1649264653814] WARN (12345 on foo): something broke when handling foo.js
  data: {
    "foo": "bar"
  }
  err: {
    "type": "Error",
    "message": "yikes!",
    "stack":
        Error: yikes!
            at Object.<anonymous> (/path/to/file.js:5:13)
            at Module._compile (internal/modules/cjs/loader.js:1085:14)
            at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10)
            at Module.load (internal/modules/cjs/loader.js:950:32)
            at Function.Module._load (internal/modules/cjs/loader.js:790:12)
            at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:75:12)
            at internal/main/run_main_module.js:17:47
  }

Objects

Rather than including information in the log message itself, it is more helpful to include data in the first argument.

For example, if we want to log the contents of an object:

const route = { method: 'GET', path: '/users/:id', handler: 'getUser' };
logger.info({ route }, 'route registered');
// instead of
logger.info('route registered: %o', route);
Output (Click to expand)
[1649265443612] INFO (12345 on foo): route registered
  route: {
    "method": "GET",
    "path": "/users/:id",
    "handler": "getUser"
  }

[1649265443613] INFO (12345 on foo): route registered: {"method":"GET","path":"/users/:id","handler":"getUser"}

Namespacing

When using the logger in another module, you could instantiate a namespace using the child method.

For example, in the @contrast/rewriter module, we could namespace the logger as follows:

const logger = core.logger.child({ name: 'contrast:rewriter' });

版本与发布记录

@contrast/logger 共发布 72 个版本,首次发布于 2022/06/16;过去 12 个月发布了 19 个版本。

版本发布日期距今
1.44.1latest2026/11/056 天前
1.44.02026/10/3011 天前
1.43.02026/10/2318 天前
1.42.02026/10/1724 天前
1.41.02026/08/192 个月前
1.40.02026/07/313 个月前

关于 @contrast/logger 的常见问题

@contrast/logger 是什么?

Centralized logging for Contrast agent services

如何安装 @contrast/logger?

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

@contrast/logger 支持 TypeScript 吗?

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

@contrast/logger 支持 ESM 还是 CommonJS?

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

@contrast/logger 需要什么版本的 Node.js?

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

@contrast/logger 可以商用吗?

@contrast/logger 使用 SEE LICENSE IN LICENSE 许可。许可条款写在包内的独立文件中,请阅读该文件确认可否商用。以上为概括,不构成法律意见,请以许可原文为准。

@contrast/logger 的下载量有多少?

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

@contrast/logger 还在维护吗?

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