1
- import { randomInt } from 'node:crypto' ;
2
- import { Console } from 'node:console' ;
3
- import type { Context , Handler } from 'aws-lambda' ;
4
1
import { Utility } from '@aws-lambda-powertools/commons' ;
5
- import { PowertoolsLogFormatter } from './formatter/PowertoolsLogFormatter.js' ;
6
- import { LogFormatterInterface } from './formatter/LogFormatterInterface.js' ;
7
- import { LogItem } from './log/LogItem.js' ;
2
+ import type { HandlerMethodDecorator } from '@aws-lambda-powertools/commons/types' ;
3
+ import type { Context , Handler } from 'aws-lambda' ;
8
4
import merge from 'lodash.merge' ;
9
- import { ConfigServiceInterface } from './config/ConfigServiceInterface.js' ;
5
+ import { Console } from 'node:console' ;
6
+ import { randomInt } from 'node:crypto' ;
10
7
import { EnvironmentVariablesService } from './config/EnvironmentVariablesService.js' ;
11
- import { LogJsonIndent } from './types/Logger.js' ;
8
+ import { LogJsonIndent } from './constants.js' ;
9
+ import { LogItem } from './formatter/LogItem.js' ;
10
+ import { PowertoolsLogFormatter } from './formatter/PowertoolsLogFormatter.js' ;
11
+ import type { ConfigServiceInterface } from './types/ConfigServiceInterface.js' ;
12
12
import type {
13
13
Environment ,
14
14
LogAttributes ,
15
15
LogLevel ,
16
16
LogLevelThresholds ,
17
+ LogFormatterInterface ,
17
18
} from './types/Log.js' ;
18
19
import type {
19
- ClassThatLogs ,
20
- HandlerMethodDecorator ,
21
- LambdaFunctionContext ,
20
+ LogFunction ,
22
21
ConstructorOptions ,
22
+ InjectLambdaContextOptions ,
23
23
LogItemExtraInput ,
24
24
LogItemMessage ,
25
- PowertoolLogData ,
26
- HandlerOptions ,
25
+ LoggerInterface ,
26
+ PowertoolsLogData ,
27
27
} from './types/Logger.js' ;
28
+
28
29
/**
29
30
* ## Intro
30
31
* The Logger utility provides an opinionated logger with output structured as JSON.
@@ -111,7 +112,7 @@ import type {
111
112
* @implements {ClassThatLogs}
112
113
* @see https://docs.powertools.aws.dev/lambda-typescript/latest/core/logger/
113
114
*/
114
- class Logger extends Utility implements ClassThatLogs {
115
+ class Logger extends Utility implements LoggerInterface {
115
116
/**
116
117
* Console instance used to print logs.
117
118
*
@@ -155,9 +156,9 @@ class Logger extends Utility implements ClassThatLogs {
155
156
SILENT : 28 ,
156
157
} ;
157
158
158
- private persistentLogAttributes ? : LogAttributes = { } ;
159
+ private persistentLogAttributes : LogAttributes = { } ;
159
160
160
- private powertoolLogData : PowertoolLogData = < PowertoolLogData > { } ;
161
+ private powertoolsLogData : PowertoolsLogData = < PowertoolsLogData > { } ;
161
162
162
163
/**
163
164
* Log level used by the current instance of Logger.
@@ -187,17 +188,15 @@ class Logger extends Utility implements ClassThatLogs {
187
188
* @returns {void }
188
189
*/
189
190
public addContext ( context : Context ) : void {
190
- const lambdaContext : Partial < LambdaFunctionContext > = {
191
- invokedFunctionArn : context . invokedFunctionArn ,
192
- coldStart : this . getColdStart ( ) ,
193
- awsRequestId : context . awsRequestId ,
194
- memoryLimitInMB : Number ( context . memoryLimitInMB ) ,
195
- functionName : context . functionName ,
196
- functionVersion : context . functionVersion ,
197
- } ;
198
-
199
- this . addToPowertoolLogData ( {
200
- lambdaContext,
191
+ this . addToPowertoolsLogData ( {
192
+ lambdaContext : {
193
+ invokedFunctionArn : context . invokedFunctionArn ,
194
+ coldStart : this . getColdStart ( ) ,
195
+ awsRequestId : context . awsRequestId ,
196
+ memoryLimitInMB : context . memoryLimitInMB ,
197
+ functionName : context . functionName ,
198
+ functionVersion : context . functionVersion ,
199
+ } ,
201
200
} ) ;
202
201
}
203
202
@@ -229,23 +228,27 @@ class Logger extends Utility implements ClassThatLogs {
229
228
* @returns {Logger }
230
229
*/
231
230
public createChild ( options : ConstructorOptions = { } ) : Logger {
232
- const parentsOptions = {
233
- logLevel : this . getLevelName ( ) ,
234
- customConfigService : this . getCustomConfigService ( ) ,
235
- logFormatter : this . getLogFormatter ( ) ,
236
- sampleRateValue : this . powertoolLogData . sampleRateValue ,
237
- } ;
238
- const parentsPowertoolsLogData = this . getPowertoolLogData ( ) ;
239
231
const childLogger = this . createLogger (
240
- merge ( parentsOptions , parentsPowertoolsLogData , options )
232
+ // Merge parent logger options with options passed to createChild,
233
+ // the latter having precedence.
234
+ merge (
235
+ { } ,
236
+ {
237
+ logLevel : this . getLevelName ( ) ,
238
+ serviceName : this . powertoolsLogData . serviceName ,
239
+ sampleRateValue : this . powertoolsLogData . sampleRateValue ,
240
+ logFormatter : this . getLogFormatter ( ) ,
241
+ customConfigService : this . getCustomConfigService ( ) ,
242
+ environment : this . powertoolsLogData . environment ,
243
+ persistentLogAttributes : this . persistentLogAttributes ,
244
+ } ,
245
+ options
246
+ )
241
247
) ;
242
-
243
- const parentsPersistentLogAttributes = this . getPersistentLogAttributes ( ) ;
244
- childLogger . addPersistentLogAttributes ( parentsPersistentLogAttributes ) ;
245
-
246
- if ( parentsPowertoolsLogData . lambdaContext ) {
247
- childLogger . addContext ( parentsPowertoolsLogData . lambdaContext as Context ) ;
248
- }
248
+ if ( this . powertoolsLogData . lambdaContext )
249
+ childLogger . addContext (
250
+ this . powertoolsLogData . lambdaContext as unknown as Context
251
+ ) ;
249
252
250
253
return childLogger ;
251
254
}
@@ -315,7 +318,7 @@ class Logger extends Utility implements ClassThatLogs {
315
318
* @returns {LogAttributes }
316
319
*/
317
320
public getPersistentLogAttributes ( ) : LogAttributes {
318
- return this . persistentLogAttributes as LogAttributes ;
321
+ return this . persistentLogAttributes ;
319
322
}
320
323
321
324
/**
@@ -361,7 +364,9 @@ class Logger extends Utility implements ClassThatLogs {
361
364
* @see https://www.typescriptlang.org/docs/handbook/decorators.html#method-decorators
362
365
* @returns {HandlerMethodDecorator }
363
366
*/
364
- public injectLambdaContext ( options ?: HandlerOptions ) : HandlerMethodDecorator {
367
+ public injectLambdaContext (
368
+ options ?: InjectLambdaContextOptions
369
+ ) : HandlerMethodDecorator {
365
370
return ( _target , _propertyKey , descriptor ) => {
366
371
/**
367
372
* The descriptor.value is the method this decorator decorates, it cannot be undefined.
@@ -409,7 +414,7 @@ class Logger extends Utility implements ClassThatLogs {
409
414
public static injectLambdaContextAfterOrOnError (
410
415
logger : Logger ,
411
416
initialPersistentAttributes : LogAttributes ,
412
- options ?: HandlerOptions
417
+ options ?: InjectLambdaContextOptions
413
418
) : void {
414
419
if ( options && options . clearState === true ) {
415
420
logger . setPersistentLogAttributes ( initialPersistentAttributes ) ;
@@ -420,13 +425,13 @@ class Logger extends Utility implements ClassThatLogs {
420
425
logger : Logger ,
421
426
event : unknown ,
422
427
context : Context ,
423
- options ?: HandlerOptions
428
+ options ?: InjectLambdaContextOptions
424
429
) : void {
425
430
logger . addContext ( context ) ;
426
431
427
432
let shouldLogEvent = undefined ;
428
- if ( options && options . hasOwnProperty ( 'logEvent' ) ) {
429
- shouldLogEvent = options . logEvent ;
433
+ if ( Object . hasOwn ( options || { } , 'logEvent' ) ) {
434
+ shouldLogEvent = options ! . logEvent ;
430
435
}
431
436
logger . logEventIfEnabled ( event , shouldLogEvent ) ;
432
437
}
@@ -439,9 +444,7 @@ class Logger extends Utility implements ClassThatLogs {
439
444
* @returns {void }
440
445
*/
441
446
public logEventIfEnabled ( event : unknown , overwriteValue ?: boolean ) : void {
442
- if ( ! this . shouldLogEvent ( overwriteValue ) ) {
443
- return ;
444
- }
447
+ if ( ! this . shouldLogEvent ( overwriteValue ) ) return ;
445
448
this . info ( 'Lambda invocation event' , { event } ) ;
446
449
}
447
450
@@ -453,7 +456,7 @@ class Logger extends Utility implements ClassThatLogs {
453
456
* @returns {void }
454
457
*/
455
458
public refreshSampleRateCalculation ( ) : void {
456
- this . setInitialSampleRate ( this . powertoolLogData . sampleRateValue ) ;
459
+ this . setInitialSampleRate ( this . powertoolsLogData . sampleRateValue ) ;
457
460
}
458
461
459
462
/**
@@ -473,11 +476,11 @@ class Logger extends Utility implements ClassThatLogs {
473
476
* @returns {void }
474
477
*/
475
478
public removePersistentLogAttributes ( keys : string [ ] ) : void {
476
- keys . forEach ( ( key ) => {
477
- if ( this . persistentLogAttributes && key in this . persistentLogAttributes ) {
479
+ for ( const key of keys ) {
480
+ if ( Object . hasOwn ( this . persistentLogAttributes , key ) ) {
478
481
delete this . persistentLogAttributes [ key ] ;
479
482
}
480
- } ) ;
483
+ }
481
484
}
482
485
483
486
/**
@@ -559,16 +562,12 @@ class Logger extends Utility implements ClassThatLogs {
559
562
/**
560
563
* It stores information that is printed in all log items.
561
564
*
562
- * @param {Partial<PowertoolLogData > } attributesArray
565
+ * @param {Partial<PowertoolsLogData > } attributes
563
566
* @private
564
567
* @returns {void }
565
568
*/
566
- private addToPowertoolLogData (
567
- ...attributesArray : Array < Partial < PowertoolLogData > >
568
- ) : void {
569
- attributesArray . forEach ( ( attributes : Partial < PowertoolLogData > ) => {
570
- merge ( this . powertoolLogData , attributes ) ;
571
- } ) ;
569
+ private addToPowertoolsLogData ( attributes : Partial < PowertoolsLogData > ) : void {
570
+ merge ( this . powertoolsLogData , attributes ) ;
572
571
}
573
572
574
573
/**
@@ -595,7 +594,7 @@ class Logger extends Utility implements ClassThatLogs {
595
594
message : typeof input === 'string' ? input : input . message ,
596
595
xRayTraceId : this . envVarsService . getXrayTraceId ( ) ,
597
596
} ,
598
- this . getPowertoolLogData ( )
597
+ this . getPowertoolsLogData ( )
599
598
) ;
600
599
601
600
let additionalLogAttributes : LogAttributes = { } ;
@@ -665,15 +664,15 @@ class Logger extends Utility implements ClassThatLogs {
665
664
* @returns - The name of the log level
666
665
*/
667
666
private getLogLevelNameFromNumber ( logLevel : number ) : Uppercase < LogLevel > {
668
- const found = Object . entries ( this . logLevelThresholds ) . find (
669
- ( [ key , value ] ) => {
670
- if ( value === logLevel ) {
671
- return key ;
672
- }
667
+ let found ;
668
+ for ( const [ key , value ] of Object . entries ( this . logLevelThresholds ) ) {
669
+ if ( value === logLevel ) {
670
+ found = key ;
671
+ break ;
673
672
}
674
- ) ! ;
673
+ }
675
674
676
- return found [ 0 ] as Uppercase < LogLevel > ;
675
+ return found as Uppercase < LogLevel > ;
677
676
}
678
677
679
678
/**
@@ -683,8 +682,8 @@ class Logger extends Utility implements ClassThatLogs {
683
682
* @private
684
683
* @returns {LogAttributes }
685
684
*/
686
- private getPowertoolLogData ( ) : PowertoolLogData {
687
- return this . powertoolLogData ;
685
+ private getPowertoolsLogData ( ) : PowertoolsLogData {
686
+ return this . powertoolsLogData ;
688
687
}
689
688
690
689
/**
@@ -765,7 +764,7 @@ class Logger extends Utility implements ClassThatLogs {
765
764
logLevel === 24
766
765
? 'error'
767
766
: ( this . getLogLevelNameFromNumber ( logLevel ) . toLowerCase ( ) as keyof Omit <
768
- ClassThatLogs ,
767
+ LogFunction ,
769
768
'critical'
770
769
> ) ;
771
770
@@ -890,14 +889,14 @@ class Logger extends Utility implements ClassThatLogs {
890
889
* @returns {void }
891
890
*/
892
891
private setInitialSampleRate ( sampleRateValue ?: number ) : void {
893
- this . powertoolLogData . sampleRateValue = 0 ;
892
+ this . powertoolsLogData . sampleRateValue = 0 ;
894
893
const constructorValue = sampleRateValue ;
895
894
const customConfigValue =
896
895
this . getCustomConfigService ( ) ?. getSampleRateValue ( ) ;
897
896
const envVarsValue = this . getEnvVarsService ( ) . getSampleRateValue ( ) ;
898
897
for ( const value of [ constructorValue , customConfigValue , envVarsValue ] ) {
899
898
if ( this . isValidSampleRate ( value ) ) {
900
- this . powertoolLogData . sampleRateValue = value ;
899
+ this . powertoolsLogData . sampleRateValue = value ;
901
900
902
901
if ( value && randomInt ( 0 , 100 ) / 100 <= value ) {
903
902
this . setLogLevel ( 'DEBUG' ) ;
@@ -972,7 +971,7 @@ class Logger extends Utility implements ClassThatLogs {
972
971
this . setCustomConfigService ( customConfigService ) ;
973
972
this . setInitialLogLevel ( logLevel ) ;
974
973
this . setLogFormatter ( logFormatter ) ;
975
- this . setPowertoolLogData ( serviceName , environment ) ;
974
+ this . setPowertoolsLogData ( serviceName , environment ) ;
976
975
this . setInitialSampleRate ( sampleRateValue ) ;
977
976
this . setLogEvent ( ) ;
978
977
this . setLogIndentation ( ) ;
@@ -991,26 +990,24 @@ class Logger extends Utility implements ClassThatLogs {
991
990
* @private
992
991
* @returns {void }
993
992
*/
994
- private setPowertoolLogData (
993
+ private setPowertoolsLogData (
995
994
serviceName ?: string ,
996
995
environment ?: Environment ,
997
996
persistentLogAttributes : LogAttributes = { }
998
997
) : void {
999
- this . addToPowertoolLogData (
1000
- {
1001
- awsRegion : this . getEnvVarsService ( ) . getAwsRegion ( ) ,
1002
- environment :
1003
- environment ||
1004
- this . getCustomConfigService ( ) ?. getCurrentEnvironment ( ) ||
1005
- this . getEnvVarsService ( ) . getCurrentEnvironment ( ) ,
1006
- serviceName :
1007
- serviceName ||
1008
- this . getCustomConfigService ( ) ?. getServiceName ( ) ||
1009
- this . getEnvVarsService ( ) . getServiceName ( ) ||
1010
- this . getDefaultServiceName ( ) ,
1011
- } ,
1012
- persistentLogAttributes
1013
- ) ;
998
+ this . addToPowertoolsLogData ( {
999
+ awsRegion : this . getEnvVarsService ( ) . getAwsRegion ( ) ,
1000
+ environment :
1001
+ environment ||
1002
+ this . getCustomConfigService ( ) ?. getCurrentEnvironment ( ) ||
1003
+ this . getEnvVarsService ( ) . getCurrentEnvironment ( ) ,
1004
+ serviceName :
1005
+ serviceName ||
1006
+ this . getCustomConfigService ( ) ?. getServiceName ( ) ||
1007
+ this . getEnvVarsService ( ) . getServiceName ( ) ||
1008
+ this . getDefaultServiceName ( ) ,
1009
+ } ) ;
1010
+ this . addPersistentLogAttributes ( persistentLogAttributes ) ;
1014
1011
}
1015
1012
}
1016
1013
0 commit comments