@emartech/json-logger

中文文档
9.2.5发布于 2026/06/01MITTypeScriptCommonJS构建来源已验证近 30 天下载 9.3万

Tiny and fast json logger with namespace support

原始使用文档
文档目录5 节
  1. Installation
  2. Usage
  3. API
  4. Log levels
  5. Logging request identifier automatically

@emartech/json-logger

A tiny and fast logging library that outputs logs in JSON format. It has the same namespace based enabling/disabling mechanism as debug.

Installation

npm install @emartech/json-logger

Usage

Since 8.0.0, by default ECS fields will be used when logging.

If for reason you still need the old format, you need to override the outputFormat config. configure will apply this setting globally, for all instances of the logger.

const { createLogger } = require('@emartech/json-logger');

createLogger.configure({
  outputFormat: 'legacy'
});

Script

process.env.DEBUG = 'redis';
const { createLogger } = require('@emartech/json-logger');
const mongoLogger = createLogger('mongo');
const redisLogger = createLogger('redis');

redisLogger.info('connected', { domain: 'yahoo' });
// ECS format: {"event":{"action":"connected","created":"2016-08-15T08:50:23.566Z"},"log":{"logger":"redis","level":30},"domain":"yahoo"}
// Legacy format: {"name":"redis","action":"connected","level":30,"time":"2016-08-15T08:50:23.566Z","domain":"yahoo"}

mongoLogger.info('connected', { domain: 'google' });
// no output, because 'mongo' is not within namespaces (process.env.DEBUG)

redisLogger.fromError('query', new Error('Unauthorized'), { problem: 'missmatch' });
// ECS format: {"event":{"action":"query","created":"2016-08-15T08:50:23.569Z"},"log":{"logger":"redis","level":50},"error":{"type":"Error","message":"Unauthorized","stack_trace":"..."},"problem":"mismatch"}
// Legacy format: {"name":"redis","action":"query","level":50,"time":"2016-08-15T08:50:23.569Z","error_name":"Error","error_stack":"Error: Unauthorized\n    at Object.<anonymous> (/home/blacksonic/workspace/bunyan-debug/example.js:15:32)\n    at Module._compile (module.js:541:32)\n    at Object.Module._extensions..js (module.js:550:10)\n    at Module.load (module.js:458:32)\n    at tryModuleLoad (module.js:417:12)\n    at Function.Module._load (module.js:409:3)\n    at Module.runMain (module.js:575:10)\n    at run (bootstrap_node.js:352:7)\n    at startup (bootstrap_node.js:144:9)\n    at bootstrap_node.js:467:3","error_message":"Unauthorized","problem":"missmatch"}

Class

const { createLogger } = require('@emartech/json-logger');
const logger = createLogger('Exporter');

class Exporter {
  export() {
    mongoLogger.info('export', { customer_id: 123 });
  }
}
import { createLogger } from '@emartech/json-logger';
const logger = createLogger('Exporter');

class Exporter {
  export() {
    mongoLogger.info('export', { customer_id: 123 });
  }
}

Tests

import { Logger } from '@emartech/json-logger';

describe('Exporter', () => {
  it('should log', () => {
    jest.spyOn(Logger.prototype, 'info').mockReturnValue();
    const exporter = new Exporter();
    
    exporter.export();

    expect(Logger.prototype.info).toHaveBeenCalledWith('export', { customer_id: 123 });
  })
});

More examples can be found in the examples directory.

API

JsonLogger(namespace)

The default export of the library acts as a factory method. Returns a logging instance with the given namespace. The DEBUG environment variable is then used to enable these instances based on comma-delimited names. Disabled instances output no logs.

process.env.DEBUG = 'redis,mysql';
const { createLogger } = require('@emartech/json-logger');

const mongoLogger = createLogger('mongo');
// mongo instance will be disabled

const redisLogger = createLogger('redis');
// redis instance will be enabled
JsonLogger.prototype.info(action, data)

Prints the provided data to the console in JSON format.

const { createLogger } = require('@emartech/json-logger');
const redisLogger = createLogger('redis');

redisLogger.info('connected', { domain: 'yahoo' });
// ECS format: {"event":{"action":"connected","created":"2016-08-15T08:50:23.566Z"},"log":{"logger":"redis","level":30},"domain":"yahoo"}
// Legacy format: {"name":"redis","action":"connected","level":30,"time":"2016-08-15T08:50:23.566Z","domain":"yahoo"}

redisLogger.info('connected');
// ECS format: {"event":{"action":"connected","created":"2016-08-15T08:50:23.566Z"},"log":{"logger":"redis","level":30}}
// Legacy format: {"name":"redis","action":"connected","level":30,"time":"2016-08-15T08:50:23.566Z"}

By default displays the namespace of the instance (name), the current time in ISO8601 format (time), the action passed to the log method and the log level associated with the method. The second argument is assigned to these basic fields and is displayed along with them.

JsonLogger.prototype.trace(action, data)

Same as info with trace log level.

JsonLogger.prototype.debug(action, data)

Same as info with debug log level.

JsonLogger.prototype.warn(action, data)

Same as info with warn log level.

JsonLogger.prototype.error(action, data)

Same as info with error log level.

JsonLogger.prototype.fatal(action, data)

Same as info with fatal log level.

JsonLogger.prototype.fromError(action, data)

Displays an error object which formatted to fit into one line. The displayed line contains the stack trace, the name and the message of the error (for Axios errors, also request and response details). The log level defaults to error.

const { createLogger } = require('@emartech/json-logger');
const redisLogger = createLogger('redis');

redisLogger.fromError('query', new Error('Unauthorized'), { problem: 'missmatch' });
// ECS format: {"event":{"action":"query","created":"2016-08-15T08:50:23.569Z"},"log":{"logger":"redis","level":50},"error":{"type":"Error","message":"Unauthorized","stack_trace":"..."},"problem":"mismatch"}
// Legacy format: {"name":"redis","action":"query","level":50,"time":"2016-08-15T08:50:23.569Z","error_name":"Error","error_stack":"Error: Unauthorized\n    at Object.<anonymous> (/home/blacksonic/workspace/bunyan-debug/example.js:15:32)\n    at Module._compile (module.js:541:32)\n    at Object.Module._extensions..js (module.js:550:10)\n    at Module.load (module.js:458:32)\n    at tryModuleLoad (module.js:417:12)\n    at Function.Module._load (module.js:409:3)\n    at Module.runMain (module.js:575:10)\n    at run (bootstrap_node.js:352:7)\n    at startup (bootstrap_node.js:144:9)\n    at bootstrap_node.js:467:3","error_message":"Unauthorized","problem":"missmatch"}
JsonLogger.prototype.warnFromError(action, data)

Same as fromError, but with warn log level.

JsonLogger.prototype.timer()

Creates a new instance of timer that has the same methods as the logging instance (info, warn, error, fromError etc.) but also logs the elapsed time in milliseconds from the creation of the instance. The elapsed time will be logged into the duration field.

const { createLogger } = require('@emartech/json-logger');
const redisLogger = createLogger('redis');

const timer = redisLogger.timer();

// heavy task

timer.info('completed');
// Legacy format: {"name":"redis","action":"completed","level":30,"time":"2016-08-15T08:50:23.566Z","duration": 1500}
// ECS format: {"event":{"action":"completed","duration":"1500","created":"2016-08-15T08:50:23.566Z"},"log":{"logger":"redis","level":30}}
JsonLogger.configure(options)

The separate steps of the logging process can be configured here. These modifications affect all the instances of the library. With transformers we can alter the data to be logged before passing to the formatter and then to the output. It is a perfect place to add the name of the machine is running on or the request id associated with the current thread stored on a continuation local storage. With the 'enhancedStackTrace' option set to true, the error stack traces will include the causes of the errors as well (if any) including their stack traces with a significantly improved truncation limit.

const { createLogger } = require('@emartech/json-logger');

createLogger.configure({
  formatter: JSON.stringify,
  output: console.log,
  transformers: [],
  outputFormat: 'ecs',
  enhancedStackTrace: true
});

Log levels

  • "fatal" (60): The service/app is going to stop or become unusable now. An operator should definitely look into this soon.
  • "error" (50): Fatal for a particular request, but the service/app continues servicing other requests. An operator should look at this soon(ish).
  • "warn" (40): A note on something that should probably be looked at by an operator eventually.
  • "info" (30): Detail on regular operation.
  • "debug" (20): Anything else, i.e. too verbose to be included in "info" level.
  • "trace" (10): Logging from external libraries used by your app or very detailed application logging.

Logging request identifier automatically

You need to use the middlewares of @emartech/cls-adapter and add its transformer to the logger's configure method. This way it will log the request identifier coming from the header field (X-Request-Id) to every log line where the called function is originating from the route handler.

For automating

const Koa = require('koa');
const { createLogger } = require('@emartech/json-logger');
const clsAdapter = require('@emartech/cls-adapter');
const logger = createLogger('redis');

createLogger.configure({
  transformers: [
    clsAdapter.addContextStorageToInput()
  ]
});

const app = new Koa();
app.use(clsAdapter.getKoaMiddleware());

app.use(async () => {
  logger.info('connected');
  // Legacy format: {"name":"redis","action":"connected","level":30,"time":"2016-08-15T08:50:23.566Z","request_id":"d5caaa0e-b04e-4d94-bc88-3ed3b62dc94a"}
})

版本与发布记录

@emartech/json-logger 共发布 57 个版本,首次发布于 2017/09/11;过去 12 个月发布了 5 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
9.2.5latest2026/06/015 个月前
9.2.42026/05/066 个月前
9.2.32026/04/087 个月前
9.2.22026/03/098 个月前
9.2.12026/01/229 个月前
9.2.02025/10/281 年前

关于 @emartech/json-logger 的常见问题

@emartech/json-logger 是什么?

Tiny and fast json logger with namespace support

如何安装 @emartech/json-logger?

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

@emartech/json-logger 支持 TypeScript 吗?

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

@emartech/json-logger 支持 ESM 还是 CommonJS?

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

@emartech/json-logger 需要什么版本的 Node.js?

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

@emartech/json-logger 可以商用吗?

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

@emartech/json-logger 的下载量有多少?

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

@emartech/json-logger 还在维护吗?

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