Skip to content

Commit 6f99073

Browse files
authored
fix(logger): handle illegal null/undefined as extra args (#3614)
1 parent e03ffc0 commit 6f99073

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

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

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Console } from 'node:console';
22
import { randomInt } from 'node:crypto';
3-
import { Utility } from '@aws-lambda-powertools/commons';
3+
import { Utility, isNullOrUndefined } from '@aws-lambda-powertools/commons';
44
import type {
55
AsyncHandler,
66
HandlerMethodDecorator,
@@ -777,6 +777,9 @@ class Logger extends Utility implements LoggerInterface {
777777
additionalAttributes: LogAttributes
778778
): void {
779779
for (const item of extraInput) {
780+
if (isNullOrUndefined(item)) {
781+
continue;
782+
}
780783
if (item instanceof Error) {
781784
additionalAttributes.error = item;
782785
} else if (typeof item === 'string') {

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

+21-1
Original file line numberDiff line numberDiff line change
@@ -601,7 +601,7 @@ describe('Working with keys', () => {
601601

602602
it('logs a warning when using both the deprecated persistentLogAttributes and persistentKeys options', () => {
603603
// Prepare
604-
const logger = new Logger({
604+
new Logger({
605605
persistentKeys: {
606606
foo: 'bar',
607607
},
@@ -739,4 +739,24 @@ describe('Working with keys', () => {
739739
})
740740
);
741741
});
742+
743+
it.each([{ value: null }, { value: undefined }])(
744+
'handles null and undefined values when passing them to the log method ($value)',
745+
({ value }) => {
746+
// Prepare
747+
const logger = new Logger();
748+
749+
// Act
750+
// @ts-expect-error - these values are already forbidden by TypeScript, but JavaScript-only customers might pass them
751+
logger.info('foo', value);
752+
753+
// Assess
754+
expect(console.info).toHaveLoggedNth(
755+
1,
756+
expect.objectContaining({
757+
message: 'foo',
758+
})
759+
);
760+
}
761+
);
742762
});

0 commit comments

Comments
 (0)