1
- import { randomInt } from 'node:crypto' ;
2
- import { Console } from 'node:console' ;
3
- import { format } from 'node:util' ;
4
- import type { Context , Handler } from 'aws-lambda' ;
5
1
import { Utility } from '@aws-lambda-powertools/commons' ;
6
- import { PowertoolsLogFormatter } from './formatter/PowertoolsLogFormatter.js' ;
7
- import { LogFormatterInterface } from './formatter/LogFormatterInterface.js' ;
8
- import { LogItem } from './log/LogItem.js' ;
2
+ import type { HandlerMethodDecorator } from '@aws-lambda-powertools/commons/types' ;
3
+ import type { Context , Handler } from 'aws-lambda' ;
9
4
import merge from 'lodash.merge' ;
10
- import { ConfigServiceInterface } from './config/ConfigServiceInterface.js' ;
5
+ import { Console } from 'node:console' ;
6
+ import { format } from 'node:util' ;
7
+ import { randomInt } from 'node:crypto' ;
11
8
import { EnvironmentVariablesService } from './config/EnvironmentVariablesService.js' ;
12
- import { LogJsonIndent } from './types/Logger.js' ;
9
+ import { LogJsonIndent } from './constants.js' ;
10
+ import { LogItem } from './formatter/LogItem.js' ;
11
+ import { PowertoolsLogFormatter } from './formatter/PowertoolsLogFormatter.js' ;
12
+ import type { ConfigServiceInterface } from './types/ConfigServiceInterface.js' ;
13
13
import type {
14
14
Environment ,
15
15
LogAttributes ,
16
16
LogLevel ,
17
17
LogLevelThresholds ,
18
+ LogFormatterInterface ,
18
19
} from './types/Log.js' ;
19
20
import type {
20
- ClassThatLogs ,
21
- HandlerMethodDecorator ,
22
- LambdaFunctionContext ,
21
+ LogFunction ,
23
22
ConstructorOptions ,
23
+ InjectLambdaContextOptions ,
24
24
LogItemExtraInput ,
25
25
LogItemMessage ,
26
- PowertoolLogData ,
27
- HandlerOptions ,
26
+ LoggerInterface ,
27
+ PowertoolsLogData ,
28
28
} from './types/Logger.js' ;
29
+
29
30
/**
30
31
* ## Intro
31
32
* The Logger utility provides an opinionated logger with output structured as JSON.
@@ -112,7 +113,7 @@ import type {
112
113
* @implements {ClassThatLogs}
113
114
* @see https://docs.powertools.aws.dev/lambda/typescript/latest/core/logger/
114
115
*/
115
- class Logger extends Utility implements ClassThatLogs {
116
+ class Logger extends Utility implements LoggerInterface {
116
117
/**
117
118
* Console instance used to print logs.
118
119
*
@@ -156,9 +157,9 @@ class Logger extends Utility implements ClassThatLogs {
156
157
SILENT : 28 ,
157
158
} ;
158
159
159
- private persistentLogAttributes ? : LogAttributes = { } ;
160
+ private persistentLogAttributes : LogAttributes = { } ;
160
161
161
- private powertoolLogData : PowertoolLogData = < PowertoolLogData > { } ;
162
+ private powertoolsLogData : PowertoolsLogData = < PowertoolsLogData > { } ;
162
163
163
164
/**
164
165
* Log level used by the current instance of Logger.
@@ -188,17 +189,15 @@ class Logger extends Utility implements ClassThatLogs {
188
189
* @returns {void }
189
190
*/
190
191
public addContext ( context : Context ) : void {
191
- const lambdaContext : Partial < LambdaFunctionContext > = {
192
- invokedFunctionArn : context . invokedFunctionArn ,
193
- coldStart : this . getColdStart ( ) ,
194
- awsRequestId : context . awsRequestId ,
195
- memoryLimitInMB : Number ( context . memoryLimitInMB ) ,
196
- functionName : context . functionName ,
197
- functionVersion : context . functionVersion ,
198
- } ;
199
-
200
- this . addToPowertoolLogData ( {
201
- lambdaContext,
192
+ this . addToPowertoolsLogData ( {
193
+ lambdaContext : {
194
+ invokedFunctionArn : context . invokedFunctionArn ,
195
+ coldStart : this . getColdStart ( ) ,
196
+ awsRequestId : context . awsRequestId ,
197
+ memoryLimitInMB : context . memoryLimitInMB ,
198
+ functionName : context . functionName ,
199
+ functionVersion : context . functionVersion ,
200
+ } ,
202
201
} ) ;
203
202
}
204
203
@@ -230,23 +229,27 @@ class Logger extends Utility implements ClassThatLogs {
230
229
* @returns {Logger }
231
230
*/
232
231
public createChild ( options : ConstructorOptions = { } ) : Logger {
233
- const parentsOptions = {
234
- logLevel : this . getLevelName ( ) ,
235
- customConfigService : this . getCustomConfigService ( ) ,
236
- logFormatter : this . getLogFormatter ( ) ,
237
- sampleRateValue : this . powertoolLogData . sampleRateValue ,
238
- } ;
239
- const parentsPowertoolsLogData = this . getPowertoolLogData ( ) ;
240
232
const childLogger = this . createLogger (
241
- merge ( parentsOptions , parentsPowertoolsLogData , options )
233
+ // Merge parent logger options with options passed to createChild,
234
+ // the latter having precedence.
235
+ merge (
236
+ { } ,
237
+ {
238
+ logLevel : this . getLevelName ( ) ,
239
+ serviceName : this . powertoolsLogData . serviceName ,
240
+ sampleRateValue : this . powertoolsLogData . sampleRateValue ,
241
+ logFormatter : this . getLogFormatter ( ) ,
242
+ customConfigService : this . getCustomConfigService ( ) ,
243
+ environment : this . powertoolsLogData . environment ,
244
+ persistentLogAttributes : this . persistentLogAttributes ,
245
+ } ,
246
+ options
247
+ )
242
248
) ;
243
-
244
- const parentsPersistentLogAttributes = this . getPersistentLogAttributes ( ) ;
245
- childLogger . addPersistentLogAttributes ( parentsPersistentLogAttributes ) ;
246
-
247
- if ( parentsPowertoolsLogData . lambdaContext ) {
248
- childLogger . addContext ( parentsPowertoolsLogData . lambdaContext as Context ) ;
249
- }
249
+ if ( this . powertoolsLogData . lambdaContext )
250
+ childLogger . addContext (
251
+ this . powertoolsLogData . lambdaContext as unknown as Context
252
+ ) ;
250
253
251
254
return childLogger ;
252
255
}
@@ -316,7 +319,7 @@ class Logger extends Utility implements ClassThatLogs {
316
319
* @returns {LogAttributes }
317
320
*/
318
321
public getPersistentLogAttributes ( ) : LogAttributes {
319
- return this . persistentLogAttributes as LogAttributes ;
322
+ return this . persistentLogAttributes ;
320
323
}
321
324
322
325
/**
@@ -362,7 +365,9 @@ class Logger extends Utility implements ClassThatLogs {
362
365
* @see https://www.typescriptlang.org/docs/handbook/decorators.html#method-decorators
363
366
* @returns {HandlerMethodDecorator }
364
367
*/
365
- public injectLambdaContext ( options ?: HandlerOptions ) : HandlerMethodDecorator {
368
+ public injectLambdaContext (
369
+ options ?: InjectLambdaContextOptions
370
+ ) : HandlerMethodDecorator {
366
371
return ( _target , _propertyKey , descriptor ) => {
367
372
/**
368
373
* The descriptor.value is the method this decorator decorates, it cannot be undefined.
@@ -410,7 +415,7 @@ class Logger extends Utility implements ClassThatLogs {
410
415
public static injectLambdaContextAfterOrOnError (
411
416
logger : Logger ,
412
417
initialPersistentAttributes : LogAttributes ,
413
- options ?: HandlerOptions
418
+ options ?: InjectLambdaContextOptions
414
419
) : void {
415
420
if ( options && options . clearState === true ) {
416
421
logger . setPersistentLogAttributes ( initialPersistentAttributes ) ;
@@ -421,13 +426,13 @@ class Logger extends Utility implements ClassThatLogs {
421
426
logger : Logger ,
422
427
event : unknown ,
423
428
context : Context ,
424
- options ?: HandlerOptions
429
+ options ?: InjectLambdaContextOptions
425
430
) : void {
426
431
logger . addContext ( context ) ;
427
432
428
433
let shouldLogEvent = undefined ;
429
- if ( options && options . hasOwnProperty ( 'logEvent' ) ) {
430
- shouldLogEvent = options . logEvent ;
434
+ if ( Object . hasOwn ( options || { } , 'logEvent' ) ) {
435
+ shouldLogEvent = options ! . logEvent ;
431
436
}
432
437
logger . logEventIfEnabled ( event , shouldLogEvent ) ;
433
438
}
@@ -440,9 +445,7 @@ class Logger extends Utility implements ClassThatLogs {
440
445
* @returns {void }
441
446
*/
442
447
public logEventIfEnabled ( event : unknown , overwriteValue ?: boolean ) : void {
443
- if ( ! this . shouldLogEvent ( overwriteValue ) ) {
444
- return ;
445
- }
448
+ if ( ! this . shouldLogEvent ( overwriteValue ) ) return ;
446
449
this . info ( 'Lambda invocation event' , { event } ) ;
447
450
}
448
451
@@ -454,7 +457,7 @@ class Logger extends Utility implements ClassThatLogs {
454
457
* @returns {void }
455
458
*/
456
459
public refreshSampleRateCalculation ( ) : void {
457
- this . setInitialSampleRate ( this . powertoolLogData . sampleRateValue ) ;
460
+ this . setInitialSampleRate ( this . powertoolsLogData . sampleRateValue ) ;
458
461
}
459
462
460
463
/**
@@ -474,11 +477,11 @@ class Logger extends Utility implements ClassThatLogs {
474
477
* @returns {void }
475
478
*/
476
479
public removePersistentLogAttributes ( keys : string [ ] ) : void {
477
- keys . forEach ( ( key ) => {
478
- if ( this . persistentLogAttributes && key in this . persistentLogAttributes ) {
480
+ for ( const key of keys ) {
481
+ if ( Object . hasOwn ( this . persistentLogAttributes , key ) ) {
479
482
delete this . persistentLogAttributes [ key ] ;
480
483
}
481
- } ) ;
484
+ }
482
485
}
483
486
484
487
/**
@@ -564,16 +567,12 @@ class Logger extends Utility implements ClassThatLogs {
564
567
/**
565
568
* It stores information that is printed in all log items.
566
569
*
567
- * @param {Partial<PowertoolLogData > } attributesArray
570
+ * @param {Partial<PowertoolsLogData > } attributes
568
571
* @private
569
572
* @returns {void }
570
573
*/
571
- private addToPowertoolLogData (
572
- ...attributesArray : Array < Partial < PowertoolLogData > >
573
- ) : void {
574
- attributesArray . forEach ( ( attributes : Partial < PowertoolLogData > ) => {
575
- merge ( this . powertoolLogData , attributes ) ;
576
- } ) ;
574
+ private addToPowertoolsLogData ( attributes : Partial < PowertoolsLogData > ) : void {
575
+ merge ( this . powertoolsLogData , attributes ) ;
577
576
}
578
577
579
578
private awsLogLevelShortCircuit ( selectedLogLevel ?: string ) : boolean {
@@ -624,7 +623,7 @@ class Logger extends Utility implements ClassThatLogs {
624
623
message : typeof input === 'string' ? input : input . message ,
625
624
xRayTraceId : this . envVarsService . getXrayTraceId ( ) ,
626
625
} ,
627
- this . getPowertoolLogData ( )
626
+ this . getPowertoolsLogData ( )
628
627
) ;
629
628
630
629
let additionalLogAttributes : LogAttributes = { } ;
@@ -694,15 +693,15 @@ class Logger extends Utility implements ClassThatLogs {
694
693
* @returns - The name of the log level
695
694
*/
696
695
private getLogLevelNameFromNumber ( logLevel : number ) : Uppercase < LogLevel > {
697
- const found = Object . entries ( this . logLevelThresholds ) . find (
698
- ( [ key , value ] ) => {
699
- if ( value === logLevel ) {
700
- return key ;
701
- }
696
+ let found ;
697
+ for ( const [ key , value ] of Object . entries ( this . logLevelThresholds ) ) {
698
+ if ( value === logLevel ) {
699
+ found = key ;
700
+ break ;
702
701
}
703
- ) ! ;
702
+ }
704
703
705
- return found [ 0 ] as Uppercase < LogLevel > ;
704
+ return found as Uppercase < LogLevel > ;
706
705
}
707
706
708
707
/**
@@ -712,8 +711,8 @@ class Logger extends Utility implements ClassThatLogs {
712
711
* @private
713
712
* @returns {LogAttributes }
714
713
*/
715
- private getPowertoolLogData ( ) : PowertoolLogData {
716
- return this . powertoolLogData ;
714
+ private getPowertoolsLogData ( ) : PowertoolsLogData {
715
+ return this . powertoolsLogData ;
717
716
}
718
717
719
718
/**
@@ -794,7 +793,7 @@ class Logger extends Utility implements ClassThatLogs {
794
793
logLevel === 24
795
794
? 'error'
796
795
: ( this . getLogLevelNameFromNumber ( logLevel ) . toLowerCase ( ) as keyof Omit <
797
- ClassThatLogs ,
796
+ LogFunction ,
798
797
'critical'
799
798
> ) ;
800
799
@@ -923,14 +922,14 @@ class Logger extends Utility implements ClassThatLogs {
923
922
* @returns {void }
924
923
*/
925
924
private setInitialSampleRate ( sampleRateValue ?: number ) : void {
926
- this . powertoolLogData . sampleRateValue = 0 ;
925
+ this . powertoolsLogData . sampleRateValue = 0 ;
927
926
const constructorValue = sampleRateValue ;
928
927
const customConfigValue =
929
928
this . getCustomConfigService ( ) ?. getSampleRateValue ( ) ;
930
929
const envVarsValue = this . getEnvVarsService ( ) . getSampleRateValue ( ) ;
931
930
for ( const value of [ constructorValue , customConfigValue , envVarsValue ] ) {
932
931
if ( this . isValidSampleRate ( value ) ) {
933
- this . powertoolLogData . sampleRateValue = value ;
932
+ this . powertoolsLogData . sampleRateValue = value ;
934
933
935
934
if ( value && randomInt ( 0 , 100 ) / 100 <= value ) {
936
935
this . setLogLevel ( 'DEBUG' ) ;
@@ -1005,7 +1004,7 @@ class Logger extends Utility implements ClassThatLogs {
1005
1004
this . setCustomConfigService ( customConfigService ) ;
1006
1005
this . setInitialLogLevel ( logLevel ) ;
1007
1006
this . setLogFormatter ( logFormatter ) ;
1008
- this . setPowertoolLogData ( serviceName , environment ) ;
1007
+ this . setPowertoolsLogData ( serviceName , environment ) ;
1009
1008
this . setInitialSampleRate ( sampleRateValue ) ;
1010
1009
this . setLogEvent ( ) ;
1011
1010
this . setLogIndentation ( ) ;
@@ -1024,26 +1023,24 @@ class Logger extends Utility implements ClassThatLogs {
1024
1023
* @private
1025
1024
* @returns {void }
1026
1025
*/
1027
- private setPowertoolLogData (
1026
+ private setPowertoolsLogData (
1028
1027
serviceName ?: string ,
1029
1028
environment ?: Environment ,
1030
1029
persistentLogAttributes : LogAttributes = { }
1031
1030
) : void {
1032
- this . addToPowertoolLogData (
1033
- {
1034
- awsRegion : this . getEnvVarsService ( ) . getAwsRegion ( ) ,
1035
- environment :
1036
- environment ||
1037
- this . getCustomConfigService ( ) ?. getCurrentEnvironment ( ) ||
1038
- this . getEnvVarsService ( ) . getCurrentEnvironment ( ) ,
1039
- serviceName :
1040
- serviceName ||
1041
- this . getCustomConfigService ( ) ?. getServiceName ( ) ||
1042
- this . getEnvVarsService ( ) . getServiceName ( ) ||
1043
- this . getDefaultServiceName ( ) ,
1044
- } ,
1045
- persistentLogAttributes
1046
- ) ;
1031
+ this . addToPowertoolsLogData ( {
1032
+ awsRegion : this . getEnvVarsService ( ) . getAwsRegion ( ) ,
1033
+ environment :
1034
+ environment ||
1035
+ this . getCustomConfigService ( ) ?. getCurrentEnvironment ( ) ||
1036
+ this . getEnvVarsService ( ) . getCurrentEnvironment ( ) ,
1037
+ serviceName :
1038
+ serviceName ||
1039
+ this . getCustomConfigService ( ) ?. getServiceName ( ) ||
1040
+ this . getEnvVarsService ( ) . getServiceName ( ) ||
1041
+ this . getDefaultServiceName ( ) ,
1042
+ } ) ;
1043
+ this . addPersistentLogAttributes ( persistentLogAttributes ) ;
1047
1044
}
1048
1045
}
1049
1046
0 commit comments