@dazn/lambda-powertools-logger

中文文档
1.28.1发布于 2020/12/10MITTypeScriptCommonJS近 30 天下载 5.6万

Logger that is tightly integrated with the rest of the lambda-powertools

原始使用文档

lambda-powertools-logger

Logger that is tightly integrated with the rest of the lambda-powertools, and knows to automatically include any correlation IDs that have been captured with @dazn/lambda-powertools-correlation-ids.

Main features:

  • structured logging with JSON

  • includes a number of common attributes: awsRegion, functionName, functionVersion, functionMemorySize and environment

  • supports sampling of debug logs with the enableDebug function (see below for more details)

  • allow log level to be changed live via the LOG_LEVEL environment variable (allowed values are DEBUG, INFO, WARN and ERROR)

  • for WARN and ERROR logs, include errorName, errorMessage and stackTrace

Getting Started

Install from NPM: npm install @dazn/lambda-powertools-logger

API

This illustrates the API for logging:

const Log = require('@dazn/lambda-powertools-logger')

Log.debug('this is a debug message')
Log.debug('this is a debug message with attributes', { userId: 'theburningmonk' })

Log.info('this is an info message')
Log.info('this is an info message with attributes', { userId: 'theburningmonk' })

Log.warn('this is a warning message')
Log.warn('this is a warning message with attributes', { userId: 'theburningmonk' })
Log.warn('this is a warning message', new Error('oops'))
Log.warn('this is a warning message with attributes, and error details', { userId: 'theburningmonk' }, new Error('oops'))

Log.error('this is an error message')
Log.error('this is an error message with attributes', { userId: 'theburningmonk' })
Log.error('this is an error message', new Error('oops'))
Log.error('this is an error message with attributes, and error details', { userId: 'theburningmonk' }, new Error('oops'))

We don't want to leave debug logging ON in production, as there are significant impact on:

  • CloudWatch Logs cost : CloudWatch Logs charges $0.50 per GB of data ingested

  • Logz.io cost : Logz.io also charges based on data ingested as well

  • Lambda cost : there are also Lambda invocation costs for shipping logs from CloudWatch Logs to Logz.io

  • Lambda concurrency : more things being logged = more Lambda invocations to ship them to Logz.io, which can potentially use up too much of our regional quota of concurrent Lambda executions (default limit is 1000, can be raised through support ticket)

  • too much noise in the logs, making it harder to find important information

Instead, we should sample debug logs for, say, 1% of invocations.

When used with other lambda-powertools, e.g. @dazn/lambda-powertools-middleware-sample-logging, debug logging can be enabled during an invocation using enableDebug function.

The @dazn/lambda-powertools-middleware-correlation-ids middleware also supplements this behaviour by allowing you to propagate decisions to enable sample logging as a special correlation IDs. This allows an entire call chain (e.g. API Gateway -> Lambda -> Kinesis -> Lambda -> SNS -> Lambda -> HTTP -> API Gateway -> Lambda) to respect the sampling decisions.

const Log = require('@dazn/lambda-powertools-logger')
// LOG_LEVEL is set to WARN via serverless.yml

Log.debug('this is not logged')

const undoDebugLog = Log.enableDebug()

Log.debug('this is logged')

undoDebugLog()

Log.debug('this is not logged')

版本与发布记录

@dazn/lambda-powertools-logger 共发布 11 个版本,首次发布于 2019/07/24;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.28.1latest2020/12/105 年前
1.28.02020/12/105 年前
1.24.12020/06/036 年前
1.24.02020/05/306 年前
1.23.12020/04/206 年前
1.23.02020/04/076 年前

关于 @dazn/lambda-powertools-logger 的常见问题

@dazn/lambda-powertools-logger 是什么?

Logger that is tightly integrated with the rest of the lambda-powertools

如何安装 @dazn/lambda-powertools-logger?

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

@dazn/lambda-powertools-logger 支持 TypeScript 吗?

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

@dazn/lambda-powertools-logger 支持 ESM 还是 CommonJS?

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

@dazn/lambda-powertools-logger 可以商用吗?

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

@dazn/lambda-powertools-logger 的下载量有多少?

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

@dazn/lambda-powertools-logger 还在维护吗?

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