@@ -25,7 +25,10 @@ import type {
25
25
LogLevel ,
26
26
LoggerInterface ,
27
27
} from './types/Logger.js' ;
28
- import type { PowertoolsLogData } from './types/logKeys.js' ;
28
+ import type {
29
+ PowertoolsLogData ,
30
+ UnformattedAttributes ,
31
+ } from './types/logKeys.js' ;
29
32
30
33
/**
31
34
* The Logger utility provides an opinionated logger with output structured as JSON for AWS Lambda.
@@ -718,59 +721,53 @@ class Logger extends Utility implements LoggerInterface {
718
721
input : LogItemMessage ,
719
722
extraInput : LogItemExtraInput
720
723
) : LogItem {
721
- let message = '' ;
722
- let otherInput : { [ key : string ] : unknown } = { } ;
723
- if ( typeof input === 'string' ) {
724
- message = input ;
725
- } else {
726
- const { message : inputMessage , ...rest } = input ;
727
- message = inputMessage ;
728
- for ( const key of Object . keys ( rest ) ) {
729
- if ( this . #checkReservedKeyAndWarn( key ) ) {
730
- delete rest [ key ] ;
731
- }
732
- }
733
- otherInput = rest ;
734
- }
735
-
736
- // create base attributes
737
- const unformattedBaseAttributes = {
724
+ const unformattedBaseAttributes : UnformattedAttributes = {
738
725
logLevel : this . getLogLevelNameFromNumber ( logLevel ) ,
739
726
timestamp : new Date ( ) ,
740
- message,
741
727
xRayTraceId : this . envVarsService . getXrayTraceId ( ) ,
742
728
...this . getPowertoolsLogData ( ) ,
729
+ message : '' ,
743
730
} ;
744
731
745
732
const additionalAttributes : LogAttributes = { } ;
746
- // gradually add additional attributes picking only the last added for each key
733
+ // Add additional attributes from persistent and temporary keys
747
734
for ( const [ key , type ] of this . #keys) {
748
- if ( type === 'persistent' ) {
749
- additionalAttributes [ key ] = this . persistentLogAttributes [ key ] ;
750
- } else {
751
- additionalAttributes [ key ] = this . temporaryLogAttributes [ key ] ;
735
+ if ( ! this . #checkReservedKeyAndWarn( key ) ) {
736
+ additionalAttributes [ key ] =
737
+ type === 'persistent'
738
+ ? this . persistentLogAttributes [ key ]
739
+ : this . temporaryLogAttributes [ key ] ;
752
740
}
753
741
}
754
742
755
- // if the main input is not a string, then it's an object with additional attributes, so we merge it
756
- merge ( additionalAttributes , otherInput ) ;
757
- // then we merge the extra input attributes (if any)
743
+ // Handle input message
744
+ if ( typeof input === 'string' ) {
745
+ unformattedBaseAttributes . message = input ;
746
+ } else {
747
+ const { message, ...rest } = input ;
748
+ unformattedBaseAttributes . message = message ;
749
+
750
+ // Add remaining input properties if they're not reserved
751
+ for ( const [ key , value ] of Object . entries ( rest ) ) {
752
+ if ( ! this . #checkReservedKeyAndWarn( key ) ) {
753
+ additionalAttributes [ key ] = value ;
754
+ }
755
+ }
756
+ }
757
+
758
+ // Handle extra input attributes
758
759
for ( const item of extraInput ) {
759
- if ( ! ( item instanceof Error ) && ! ( typeof item === 'string' ) ) {
760
- for ( const key of Object . keys ( item ) ) {
761
- if ( this . #checkReservedKeyAndWarn( key ) ) {
762
- delete item [ key ] ;
760
+ if ( item instanceof Error ) {
761
+ additionalAttributes . error = item ;
762
+ } else if ( typeof item === 'string' ) {
763
+ additionalAttributes . extra = item ;
764
+ } else {
765
+ for ( const [ key , value ] of Object . entries ( item ) ) {
766
+ if ( ! this . #checkReservedKeyAndWarn( key ) ) {
767
+ additionalAttributes [ key ] = value ;
763
768
}
764
769
}
765
770
}
766
- const attributes : LogAttributes =
767
- item instanceof Error
768
- ? { error : item }
769
- : typeof item === 'string'
770
- ? { extra : item }
771
- : item ;
772
-
773
- merge ( additionalAttributes , attributes ) ;
774
771
}
775
772
776
773
return this . getLogFormatter ( ) . formatAttributes (
0 commit comments