From 778b9ac101f9da9b7cfc5b9b46c4e57035ac9177 Mon Sep 17 00:00:00 2001 From: Andrea Amorosi Date: Tue, 18 Feb 2025 11:08:04 +0100 Subject: [PATCH 1/2] fix(logger): handle illegal null/undefined as extra args --- packages/logger/src/Logger.ts | 9 +++++++- .../logger/tests/unit/workingWithkeys.test.ts | 22 ++++++++++++++++++- 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/packages/logger/src/Logger.ts b/packages/logger/src/Logger.ts index ee1a240789..71e1c2a2b3 100644 --- a/packages/logger/src/Logger.ts +++ b/packages/logger/src/Logger.ts @@ -1,6 +1,10 @@ import { Console } from 'node:console'; import { randomInt } from 'node:crypto'; -import { Utility } from '@aws-lambda-powertools/commons'; +import { + Utility, + isNull, + isNullOrUndefined, +} from '@aws-lambda-powertools/commons'; import type { AsyncHandler, HandlerMethodDecorator, @@ -777,6 +781,9 @@ class Logger extends Utility implements LoggerInterface { additionalAttributes: LogAttributes ): void { for (const item of extraInput) { + if (isNullOrUndefined(item)) { + continue; + } if (item instanceof Error) { additionalAttributes.error = item; } else if (typeof item === 'string') { diff --git a/packages/logger/tests/unit/workingWithkeys.test.ts b/packages/logger/tests/unit/workingWithkeys.test.ts index b39c23c1f6..94c388fc55 100644 --- a/packages/logger/tests/unit/workingWithkeys.test.ts +++ b/packages/logger/tests/unit/workingWithkeys.test.ts @@ -601,7 +601,7 @@ describe('Working with keys', () => { it('logs a warning when using both the deprecated persistentLogAttributes and persistentKeys options', () => { // Prepare - const logger = new Logger({ + new Logger({ persistentKeys: { foo: 'bar', }, @@ -739,4 +739,24 @@ describe('Working with keys', () => { }) ); }); + + it.each([{ value: null }, { value: undefined }])( + 'handles null and undefined values when passing them to the log method ($value)', + ({ value }) => { + // Prepare + const logger = new Logger(); + + // Act + // @ts-expect-error - these values are already forbidden by TypeScript, but JavaScript-only customers might pass them + logger.info('foo', value); + + // Assess + expect(console.info).toHaveLoggedNth( + 1, + expect.objectContaining({ + message: 'foo', + }) + ); + } + ); }); From 383f477b9e9f593af066dd07afc1291f2ebdfda1 Mon Sep 17 00:00:00 2001 From: Andrea Amorosi Date: Tue, 18 Feb 2025 11:18:56 +0100 Subject: [PATCH 2/2] chore: remove unused import --- packages/logger/src/Logger.ts | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/packages/logger/src/Logger.ts b/packages/logger/src/Logger.ts index 71e1c2a2b3..61f2da3df7 100644 --- a/packages/logger/src/Logger.ts +++ b/packages/logger/src/Logger.ts @@ -1,10 +1,6 @@ import { Console } from 'node:console'; import { randomInt } from 'node:crypto'; -import { - Utility, - isNull, - isNullOrUndefined, -} from '@aws-lambda-powertools/commons'; +import { Utility, isNullOrUndefined } from '@aws-lambda-powertools/commons'; import type { AsyncHandler, HandlerMethodDecorator,