@5app/logger

中文文档
2.4.0发布于 2026/08/05MITCommonJS近 30 天下载 2333

Simple logger used in 5app microservices

原始使用文档
文档目录5 节
  1. Usage
  2. Options
  3. Logging levels
  4. Slack alerts
  5. Fetching context dynamically

logger

Simple console logger that outputs json in prod and pretty messages on dev

Usage

npm install --save @5app/logger
const logger = require('@5app/logger');

logger.info('An email was sent', {
  email: '[email protected]',
  template: 'template1',
});

logger.error(new Error('Unknown playlist 123'));

Options

The logger can optionally be customised using the following environment variables:

  • LOGS_FORMAT: if set to json, the logger will log messages in json format instead of pretty messages (default behaviour).
  • LOGS_LEVEL: minimum logging level, by default it will be debug. Accepted values are 'debug', 'info', 'warn', and 'error'
  • TAG: release tag (e.g. docker image tag) to be added to the log messages

Logging levels

Logging levels are (from lower to higher priority): 'debug', 'info', 'warn', and 'error'. The logger provides the logging functions with the following signatures: logger.<level>(message, objectOrError)

Here is an example of how the logger can be used:

logger.error('An error happened', new ApiError('The api call failed', 404)); // will log the message, the error message, the stack trace, and the statusCode error property
logger.warn('Be warned', {a: 1, b: Date.now(), c: 'some string'});
logger.info('An event happened', {a: 1, b: Date.now(), c: 'some string'});
logger.debug('A minor operation', {a: 1, b: Date.now(), c: 'some string'});

Slack alerts

logger.slackAlert([level], message, ...) logs like the regular level functions above, but prepends slackAlert to the message and adds a random slackAlertId (via crypto.randomUUID()) to the logged context, generating one even if no context is provided. level is optional and defaults to 'error'; when provided it must be one of 'debug', 'info', 'warn', or 'error'. Any remaining arguments are forwarded as-is to logger.<level>, so they follow that level's own signature (e.g. context and errorObject for 'error', just context for the others).

This is intended for hooking up a log-shipping pipeline (e.g. a log processor that watches for the slackAlert prefix) to post the message to Slack, using slackAlertId to correlate the Slack post back to the original log line, while still logging it at the given severity.

logger.slackAlert('Payment provider is down', error); // level defaults to "error"
logger.slackAlert('warn', 'Queue depth is high', {depth: 1200}); // context is merged with {slackAlertId: '...'}
logger.slackAlert('warn', 'Queue depth is high'); // context defaults to {slackAlertId: '...'}
logger.slackAlert('error', 'Payment provider is down', {provider: 'stripe'}, error);
logger.slackAlert('error', 'Payment provider is down', error); // the (message, errorObject) shorthand also works; slackAlertId is added alongside the error details

Note: because level is detected by checking if the first argument is a known level string, passing an unrecognised level (e.g. a typo like 'eror') is not an error — it's silently read as message instead, with level defaulting to 'error'.

Fetching context dynamically

In addition to providing a context object, you can also use logger.addContext to provide a function which will be called on every log to get a context object.

This can be helpful if you are using async_hooks or cls-hooked to store request metadata similarly to thread-local storage.

For instance, you can do the following without having to explicitly pass the correlationId to each log:

const asyncLocalStorage = new AsyncLocalStorage();

expressApp.use((req, res, next) {
  const correlationId = req.get('X-Correlation-Id') || uuidV4();
  asyncLocalStorage.run({correlationId}, () => next());
});

logger.addContext(() => asyncLocalStorage.getStore());
logger.info('User profile updated', {userId: 1234});

And the output will look like:

{"level":"info","timestamp":"2021-02-23T17:55:43.011Z","correlationId":"12bf5b37-e0b8-56e0-8dcf-dc8c4aefc123","userId":1234}

版本与发布记录

@5app/logger 共发布 12 个版本,首次发布于 2019/09/10;过去 12 个月发布了 1 个版本。

版本发布日期距今
2.4.0latest2026/08/053 个月前
2.3.12022/09/154 年前
2.3.02022/02/074 年前
2.2.02021/02/245 年前
2.1.02020/10/236 年前
2.0.22020/04/026 年前

关于 @5app/logger 的常见问题

@5app/logger 是什么?

Simple logger used in 5app microservices

如何安装 @5app/logger?

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

@5app/logger 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/5app__logger 是否存在。

@5app/logger 支持 ESM 还是 CommonJS?

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

@5app/logger 可以商用吗?

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

@5app/logger 的下载量有多少?

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

@5app/logger 还在维护吗?

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