@beforeyoubid/logger-adapter

中文文档
2.1.1发布于 2026/11/06ISCTypeScriptCommonJS近 30 天下载 3286

A platform logger module to send the log messages to Mezmo (formerly LogDNA).

原始使用文档
文档目录15 节
  1. Console Logger vs. Winston Logger
  2. Versions
  3. 1) Use Console Logger
  4. Activate it
  5. For a non-lambda environment
  6. Flush All Messages
  7. Lambda: Use a handler wrapper for Serverless Express
  8. Lambda: Use with a Callback Handler
  9. 2) Use Winston Logger
  10. Activate it
  11. For a non-lambda environment
  12. Lambda: Use a handler wrapper for Serverless Express
  13. Lambda: Use with a Callback Handler
  14. Prefixing your log message
  15. MetricLogger

Mezmo (Formerly LogDNA) Logger Adapter

Notes - LogDNA made a decision not to rename the library to Mezmo just yet. To avoid having two names, we decided to keep the variable, class and etc as LogDNA for now.


This is our BYB logger module that is designed to support multiple two logging scenarios.

  • Some repos may heavily rely on console.log() or similar to output log on the console. We can utilise this module to wrap around the original console so all logs can be sent to Mezmo as well. The service need to add consoleLogger.init() in the handler.
  • A Winston logger - Winston creates a generic logger objec that allows us to perform various operations on the log e.g. change the colour of the log line, format message, can customise/add a custom code (using a transport mechanism) to send log messages to Mezmo or store in the database if we want.

This module is designed to work on a native node runtime and in a Lambda environment. For Lambda, please see the Flush All Messages section below.

Console Logger vs. Winston Logger

  • Dispite that console.log() is not a recommended approach to be used for frontend application but it's fine to use in the backend. Here are some limitations:
    • The native console log object doesn't support the LOG_LEVEL. For example, you may not want console.debug() to show debugging info on a production server.
    • Doesn't support log colour
    • Doesn't show datetime which is hard to make sense of
    • Doesn't support to send log to external services
  • Console Logger wraps over the existing console object as a pathway to adopting a logger object.
    • Log levels
    • Send to external service
  • Winston Logger solves all limitations described above

If you are using console.log on the existing code base and not ready to switch to Winston logger just yet, this module can also helps you out as well.

Versions

  • v1.x.x (first version)
  • v2.x.x
    • Utilise the latest module actively maintained by Mezmo team (https://www.npmjs.com/package/@logdna/logger)
    • Fix a missing some log line issue occured due to the the fact that logdna-winston module creates a separate logdna client instance and it doesn't get a chance to fully flush messages before Lambda terminates. We decided to customise this module in this project and allow us to utilise a single instance of a LogDNA logger object so that ensureFlushAll() can flush those messages before we terminate Lambda function. This single LogDNA is shared and used by both console log wrapper and LogDNA transport as part of Winston log system.
    • Although it is not a major change, users of this module would like an explicably upgraded version. Just update to the most recent v2 version; no API modifications are necessary on the consumer side.
    • Also moved createLoggerObject() into this shared module so it's easier to use and prefix message before returning the actual logger object. e.g. createLoggerObject([LOG-PREFIX]);
    • V2 Diagram
  • v2.0.3 onwards
    • Adding generic MetricLogger

1) Use Console Logger

  yarn add @beforeyoubid/logger-adapter

Activate it

We follow 12-factors and we can enable and send log to LogDNA by supplying LOGDNA_KEY as part of the environment variables.

  LOGDNA_KEY=xxxxxxyyyyyyyzzzzzzz

Please note that on your local, you can set LOGDNA_KEY= to disable sending logs to LogDNA. Supply LOG_LEVEL as part of the environment variable.

Additionally, you can keep LOGDNA_KEY value the same way you set for other environments but can disable sending logs by setting LOGDNA_ENABLED=false in your env variables.

For a non-lambda environment

    import { consoleLogger } from '@beforeyoubid/logger-adapter';
    consoleLogger.init();

    // Don't need to call consoleLogger.flushAll(), logdna object has default flush interval interval at 250ms

Flush All Messages

LogDNA puts messages in the buffer and push out every 250ms. If Lambda is terminated before some of these intervals, it's likely that some of message logs are not sent to LogDNA. Use ensureFlushAll() to flush messages automatically.

Lambda: Use a handler wrapper for Serverless Express

  import { consoleLogger, ensureFlushAll } from '@beforeyoubid/logger-adapter';
  consoleLogger.init();

  export default ensureFlushAll(yourLambdaHandler);

Lambda: Use with a Callback Handler

  import { consoleLogger, ensureFlushAllCallback } from '@beforeyoubid/logger-adapter';
  consoleLogger.init();

  export default ensureFlushAllCallback(yourLambdaHandlerCallback);

2) Use Winston Logger

  yarn add @beforeyoubid/logger-adapter

Activate it

We follow 12-factors and we can enable and send log to LogDNA by supplying LOGDNA_KEY as part of the environment variables.

  LOGDNA_KEY=xxxxxxyyyyyyyzzzzzzz

Please note that on your local, you can set LOGDNA_KEY= to disable sending logs to LogDNA. Supply LOG_LEVEL as part of the environment variable.

For a non-lambda environment

    import { logger } from '@beforeyoubid/logger-adapter';

    logger.debug('This message only show up when LOG_LEVEL is set to "debug"')
    logger.info('This message only show up when LOG_LEVEL is set to "info"')

Lambda: Use a handler wrapper for Serverless Express

  import { logger, ensureFlushAll } from '@beforeyoubid/logger-adapter';

  // you can use logger.debug('log text in a string format');

  export default ensureFlushAll(yourLambdaHandler);

Lambda: Use with a Callback Handler

  import { logger, ensureFlushAllCallback } from '@beforeyoubid/logger-adapter';

  // you can use logger.debug('log text in a string format');

  export default ensureFlushAllCallback(yourLambdaHandlerCallback);


Prefixing your log message

In some cases, you may want to have a clearer log message by prefixing them with a predefined string e.g. name of your module so they look like this. Log prefix

  import { getLoggerObject } from '@beforeyoubid/logger-adapter';

  const logger = getLoggerObject('[Your Custom Prefix]');

  logger.info('the usual log string...')

Notes that this function just wraps the original Winston logger instance your normally access from this line. import { logger } from '@beforeyoubid/logger-adapter';. It only wraps and add the prefix the log line for you and returns with debug, info, warn, error functions.

MetricLogger

Motivation:

  • Mezmo is one of the leading telemetry tools for logging and debugging
  • We can also utilise Mezmo to collect, process and display business metrics based on log messages without extra fee on the existing plan
  • The setup is very simple and Mezmo does this out of the box

How does it work?

  • When a JSON string (e.g. JSON.stringify(jsonObject)) is sent to Mezmo, Mezmo is smart enough to parse this into a json object and index them automatically
  • With this process, we can search for any value in the object and/or create a dashboard on any specific metric we see fit
  • The only drawback, the data retention is usually limited to 30 days as per the data retention policy on the account

How to use:

版本与发布记录

@beforeyoubid/logger-adapter 共发布 26 个版本,首次发布于 2020/08/15;过去 12 个月发布了 2 个版本。

版本发布日期距今
2.1.1latest2026/11/061 天前
2.1.02026/11/061 天前
2.0.42024/09/182 年前
2.0.32024/09/182 年前
2.0.22024/07/022 年前
2.0.12024/07/012 年前

关于 @beforeyoubid/logger-adapter 的常见问题

@beforeyoubid/logger-adapter 是什么?

A platform logger module to send the log messages to Mezmo (formerly LogDNA).

如何安装 @beforeyoubid/logger-adapter?

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

@beforeyoubid/logger-adapter 支持 TypeScript 吗?

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

@beforeyoubid/logger-adapter 支持 ESM 还是 CommonJS?

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

@beforeyoubid/logger-adapter 可以商用吗?

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

@beforeyoubid/logger-adapter 的下载量有多少?

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

@beforeyoubid/logger-adapter 还在维护吗?

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