Skip to content

Commit

Permalink
feat(logger): add support for AWS_LAMBDA_LOG_LEVEL and `POWERTOOLS_…
Browse files Browse the repository at this point in the history
…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
  • Loading branch information
dreamorosi committed Feb 20, 2024
1 parent 0ed2ed1 commit 74e94d4
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 7 deletions.
10 changes: 5 additions & 5 deletions docs/core/logger.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@ The library has three optional settings, which can be set via environment variab

These settings will be used across all logs emitted:

| Setting | Description | Environment variable | Default Value | Allowed Values | Example Value | Constructor parameter |
| ---------------------- | ---------------------------------------------------------------------------------------------------------------- | ------------------------------- | ------------------- | ------------------------------------------------------ | ------------------- | --------------------- |
| **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` |
| **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` |
| **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` |
| Setting | Description | Environment variable | Default Value | Allowed Values | Example Value | Constructor parameter |
| ----------------- | ---------------------------------------------------------------------------------------------------------------- | ------------------------------- | ------------------- | ------------------------------------------------------ | ------------------- | --------------------- |
| **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` |
| **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` |
| **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` |

See all environment variables in the [Environment variables](../index.md/#environment-variables) section.
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"}.
Expand Down
1 change: 1 addition & 0 deletions packages/logger/src/Logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Utility } from '@aws-lambda-powertools/commons';
import type { HandlerMethodDecorator } from '@aws-lambda-powertools/commons/types';
import type { Context, Handler } from 'aws-lambda';
import merge from 'lodash.merge';
import { format } from 'node:util';
import { Console } from 'node:console';
import { format } from 'node:util';
import { randomInt } from 'node:crypto';
Expand Down
7 changes: 5 additions & 2 deletions packages/logger/tests/unit/Logger.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ describe('Class: Logger', () => {
// Prepare
const loggerOptions = undefined;
delete process.env.POWERTOOLS_SERVICE_NAME;
delete process.env.LOG_LEVEL;
delete process.env.POWERTOOLS_LOG_LEVEL;

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

// Act
const logger = new Logger(loggerOptions);
Expand Down Expand Up @@ -354,6 +354,9 @@ describe('Class: Logger', () => {
get(name: string): string {
return `a-string-from-${name}`;
},
getAwsLogLevel() {
return 'INFO';
},
getCurrentEnvironment(): string {
return 'dev';
},
Expand Down

0 comments on commit 74e94d4

Please sign in to comment.