Skip to content

Commit c87ead0

Browse files
committed
feat(logger): add support for AWS_LAMBDA_LOG_LEVEL and POWERTOOLS_LOG_LEVEL (#1795)
* feat(logger): support advanced logging * docs(logger): add alc info * feat(logger): support alc * docs: fix alc docs links * tests(logger): add unit tests for the feature * docs(logger): make POWERTOOLS_LOG_LEVEL default
1 parent d484737 commit c87ead0

File tree

3 files changed

+11
-7
lines changed

3 files changed

+11
-7
lines changed

Diff for: docs/core/logger.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,11 @@ The library has three optional settings, which can be set via environment variab
4545

4646
These settings will be used across all logs emitted:
4747

48-
| Setting | Description | Environment variable | Default Value | Allowed Values | Example Value | Constructor parameter |
49-
| ---------------------- | ---------------------------------------------------------------------------------------------------------------- | ------------------------------- | ------------------- | ------------------------------------------------------ | ------------------- | --------------------- |
50-
| **Service name** | Sets the name of service of which the Lambda function is part of, that will be present across all log statements | `POWERTOOLS_SERVICE_NAME` | `service_undefined` | Any string | `serverlessAirline` | `serviceName` |
51-
| **Logging level** | Sets how verbose Logger should be, from the most verbose to the least verbose (no logs) | `POWERTOOLS_LOG_LEVEL` | `INFO` | `DEBUG`, `INFO`, `WARN`, `ERROR`, `CRITICAL`, `SILENT` | `ERROR` | `logLevel` |
52-
| **Sample rate** | Probability that a Lambda invocation will print all the log items regardless of the log level setting | `POWERTOOLS_LOGGER_SAMPLE_RATE` | `0` | `0.0` to `1.0` | `0.1` | `sampleRateValue` |
48+
| Setting | Description | Environment variable | Default Value | Allowed Values | Example Value | Constructor parameter |
49+
| ----------------- | ---------------------------------------------------------------------------------------------------------------- | ------------------------------- | ------------------- | ------------------------------------------------------ | ------------------- | --------------------- |
50+
| **Service name** | Sets the name of service of which the Lambda function is part of, that will be present across all log statements | `POWERTOOLS_SERVICE_NAME` | `service_undefined` | Any string | `serverlessAirline` | `serviceName` |
51+
| **Logging level** | Sets how verbose Logger should be, from the most verbose to the least verbose (no logs) | `POWERTOOLS_LOG_LEVEL` | `INFO` | `DEBUG`, `INFO`, `WARN`, `ERROR`, `CRITICAL`, `SILENT` | `ERROR` | `logLevel` |
52+
| **Sample rate** | Probability that a Lambda invocation will print all the log items regardless of the log level setting | `POWERTOOLS_LOGGER_SAMPLE_RATE` | `0` | `0.0` to `1.0` | `0.1` | `sampleRateValue` |
5353

5454
See all environment variables in the [Environment variables](../index.md/#environment-variables) section.
5555
Check API docs to learn more about [Logger constructor options](https://docs.powertools.aws.dev/lambda/typescript/latest/api/types/_aws_lambda_powertools_logger.types.ConstructorOptions.html){target="_blank"}.

Diff for: packages/logger/src/Logger.ts

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { Utility } from '@aws-lambda-powertools/commons';
22
import type { HandlerMethodDecorator } from '@aws-lambda-powertools/commons/types';
33
import type { Context, Handler } from 'aws-lambda';
44
import merge from 'lodash.merge';
5+
import { format } from 'node:util';
56
import { Console } from 'node:console';
67
import { format } from 'node:util';
78
import { randomInt } from 'node:crypto';

Diff for: packages/logger/tests/unit/Logger.test.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ describe('Class: Logger', () => {
136136
// Prepare
137137
const loggerOptions = undefined;
138138
delete process.env.POWERTOOLS_SERVICE_NAME;
139-
delete process.env.LOG_LEVEL;
139+
delete process.env.POWERTOOLS_LOG_LEVEL;
140140

141141
// Act
142142
const logger = new Logger(loggerOptions);
@@ -286,7 +286,7 @@ describe('Class: Logger', () => {
286286
test('when no log level is set, returns a Logger instance with INFO level', () => {
287287
// Prepare
288288
const loggerOptions: ConstructorOptions = {};
289-
delete process.env.LOG_LEVEL;
289+
delete process.env.POWERTOOLS_LOG_LEVEL;
290290

291291
// Act
292292
const logger = new Logger(loggerOptions);
@@ -354,6 +354,9 @@ describe('Class: Logger', () => {
354354
get(name: string): string {
355355
return `a-string-from-${name}`;
356356
},
357+
getAwsLogLevel() {
358+
return 'INFO';
359+
},
357360
getCurrentEnvironment(): string {
358361
return 'dev';
359362
},

0 commit comments

Comments
 (0)