File tree Expand file tree Collapse file tree 2 files changed +25
-2
lines changed Expand file tree Collapse file tree 2 files changed +25
-2
lines changed Original file line number Diff line number Diff line change 11import { Console } from 'node:console' ;
22import { randomInt } from 'node:crypto' ;
3- import { Utility } from '@aws-lambda-powertools/commons' ;
3+ import { Utility , isNullOrUndefined } from '@aws-lambda-powertools/commons' ;
44import 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' ) {
Original file line number Diff line number Diff 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} ) ;
You can’t perform that action at this time.
0 commit comments