-
Notifications
You must be signed in to change notification settings - Fork 146
/
Logger.ts
863 lines (776 loc) · 25.7 KB
/
Logger.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
import { Console } from 'console';
import type { Context, Handler } from 'aws-lambda';
import { Utility } from '@aws-lambda-powertools/commons';
import { LogFormatterInterface, PowertoolLogFormatter } from './formatter';
import { LogItem } from './log';
import merge from 'lodash.merge';
import { ConfigServiceInterface, EnvironmentVariablesService } from './config';
import { LogJsonIndent } from './types';
import type {
ClassThatLogs,
Environment,
HandlerMethodDecorator,
LambdaFunctionContext,
LogAttributes,
ConstructorOptions,
LogItemExtraInput,
LogItemMessage,
LogLevel,
LogLevelThresholds,
PowertoolLogData,
HandlerOptions,
} from './types';
/**
* ## Intro
* The Logger utility provides an opinionated logger with output structured as JSON.
*
* ## Key features
* * Capture key fields from Lambda context, cold start and structures logging output as JSON
* * Log Lambda context when instructed (disabled by default)
* * Log sampling prints all logs for a percentage of invocations (disabled by default)
* * Append additional keys to structured log at any point in time
*
* ## Usage
*
* For more usage examples, see [our documentation](https://awslabs.github.io/aws-lambda-powertools-typescript/latest/core/logger/).
*
* ### Basic usage
*
* @example
* ```typescript
* import { Logger } from '@aws-lambda-powertools/logger';
*
* // Logger parameters fetched from the environment variables:
* const logger = new Logger();
* ```
*
* ### Functions usage with middleware
*
* If you use function-based Lambda handlers you can use the [injectLambdaContext()](#injectLambdaContext)
* middy middleware to automatically add context to your Lambda logs.
*
* @example
* ```typescript
* import { Logger, injectLambdaContext } from '@aws-lambda-powertools/logger';
* import middy from '@middy/core';
*
* const logger = new Logger();
*
* const lambdaHandler = async (_event: any, _context: any) => {
* logger.info('This is an INFO log with some context');
* };
*
* export const handler = middy(lambdaHandler).use(injectLambdaContext(logger));
* ```
*
* ### Object oriented usage with decorators
*
* If instead you use TypeScript classes to wrap your Lambda handler you can use the [@logger.injectLambdaContext()](./_aws_lambda_powertools_logger.Logger.html#injectLambdaContext) decorator.
*
* @example
* ```typescript
* import { Logger } from '@aws-lambda-powertools/logger';
* import { LambdaInterface } from '@aws-lambda-powertools/commons';
*
* const logger = new Logger();
*
* class Lambda implements LambdaInterface {
*
* // FYI: Decorator might not render properly in VSCode mouse over due to https://github.com/microsoft/TypeScript/issues/47679 and might show as *@logger* instead of `@logger.injectLambdaContext`
*
* // Decorate your handler class method
* @logger.injectLambdaContext()
* public async handler(_event: any, _context: any): Promise<void> {
* logger.info('This is an INFO log with some context');
* }
* }
*
* const handlerClass = new Lambda();
* export const handler = handlerClass.handler.bind(handlerClass);
* ```
*
* ### Functions usage with manual instrumentation
*
* If you prefer to manually instrument your Lambda handler you can use the methods in the Logger class directly.
*
* @example
* ```typescript
* import { Logger } from '@aws-lambda-powertools/logger';
*
* const logger = new Logger();
*
* export const handler = async (_event, context) => {
* logger.addContext(context);
* logger.info('This is an INFO log with some context');
* };
* ```
*
* @class
* @implements {ClassThatLogs}
* @see https://awslabs.github.io/aws-lambda-powertools-typescript/latest/core/logger/
*/
class Logger extends Utility implements ClassThatLogs {
// console is initialized in the constructor in setOptions()
private console!: Console;
private customConfigService?: ConfigServiceInterface;
private static readonly defaultLogLevel: LogLevel = 'INFO';
// envVarsService is always initialized in the constructor in setOptions()
private envVarsService!: EnvironmentVariablesService;
private logEvent: boolean = false;
private logFormatter?: LogFormatterInterface;
private logIndentation: number = LogJsonIndent.COMPACT;
private logLevel?: LogLevel;
private readonly logLevelThresholds: LogLevelThresholds = {
DEBUG: 8,
INFO: 12,
WARN: 16,
ERROR: 20,
};
private logsSampled: boolean = false;
private persistentLogAttributes?: LogAttributes = {};
private powertoolLogData: PowertoolLogData = <PowertoolLogData>{};
/**
* It initializes the Logger class with an optional set of options (settings).
* *
* @param {ConstructorOptions} options
*/
public constructor(options: ConstructorOptions = {}) {
super();
this.setOptions(options);
}
/**
* It adds the current Lambda function's invocation context data to the powertoolLogData property of the instance.
* This context data will be part of all printed log items.
*
* @param {Context} context
* @returns {void}
*/
public addContext(context: Context): void {
const lambdaContext: Partial<LambdaFunctionContext> = {
invokedFunctionArn: context.invokedFunctionArn,
coldStart: this.getColdStart(),
awsRequestId: context.awsRequestId,
memoryLimitInMB: Number(context.memoryLimitInMB),
functionName: context.functionName,
functionVersion: context.functionVersion,
};
this.addToPowertoolLogData({
lambdaContext,
});
}
/**
* It adds the given attributes (key-value pairs) to all log items generated by this Logger instance.
*
* @param {LogAttributes} attributes
* @returns {void}
*/
public addPersistentLogAttributes(attributes?: LogAttributes): void {
merge(this.persistentLogAttributes, attributes);
}
/**
* Alias for addPersistentLogAttributes.
*
* @param {LogAttributes} attributes
* @returns {void}
*/
public appendKeys(attributes?: LogAttributes): void {
this.addPersistentLogAttributes(attributes);
}
/**
* It creates a separate Logger instance, identical to the current one
* It's possible to overwrite the new instance options by passing them.
*
* @param {ConstructorOptions} options
* @returns {Logger}
*/
public createChild(options: ConstructorOptions = {}): Logger {
const parentsOptions = {
logLevel: this.getLogLevel(),
customConfigService: this.getCustomConfigService(),
logFormatter: this.getLogFormatter(),
};
const parentsPowertoolsLogData = this.getPowertoolLogData();
const childLogger = new Logger(merge(parentsOptions, parentsPowertoolsLogData, options));
const parentsPersistentLogAttributes = this.getPersistentLogAttributes();
childLogger.addPersistentLogAttributes(parentsPersistentLogAttributes);
if (parentsPowertoolsLogData.lambdaContext) {
childLogger.addContext(parentsPowertoolsLogData.lambdaContext as Context);
}
return childLogger;
}
/**
* It prints a log item with level DEBUG.
*
* @param {LogItemMessage} input
* @param {Error | LogAttributes | string} extraInput
* @returns {void}
*/
public debug(input: LogItemMessage, ...extraInput: LogItemExtraInput): void {
this.processLogItem('DEBUG', input, extraInput);
}
/**
* It prints a log item with level ERROR.
*
* @param {LogItemMessage} input
* @param {Error | LogAttributes | string} extraInput
* @returns {void}
*/
public error(input: LogItemMessage, ...extraInput: LogItemExtraInput): void {
this.processLogItem('ERROR', input, extraInput);
}
/**
* It returns a boolean value. True means that the Lambda invocation events
* are printed in the logs.
*
* @returns {boolean}
*/
public getLogEvent(): boolean {
return this.logEvent;
}
/**
* It returns a boolean value, if true all the logs will be printed.
*
* @returns {boolean}
*/
public getLogsSampled(): boolean {
return this.logsSampled;
}
/**
* It returns the persistent log attributes, which are the attributes
* that will be logged in all log items.
*
* @private
* @returns {LogAttributes}
*/
public getPersistentLogAttributes(): LogAttributes {
return <LogAttributes> this.persistentLogAttributes;
}
/**
* It prints a log item with level INFO.
*
* @param {LogItemMessage} input
* @param {Error | LogAttributes | string} extraInput
* @returns {void}
*/
public info(input: LogItemMessage, ...extraInput: LogItemExtraInput): void {
this.processLogItem('INFO', input, extraInput);
}
/**
* Method decorator that adds the current Lambda function context as extra
* information in all log items.
*
* The decorator can be used only when attached to a Lambda function handler which
* is written as method of a class, and should be declared just before the handler declaration.
*
* Note: Currently TypeScript only supports decorators on classes and methods. If you are using the
* function syntax, you should use the middleware instead.
*
* @example
* ```typescript
* import { Logger } from '@aws-lambda-powertools/logger';
* import { LambdaInterface } from '@aws-lambda-powertools/commons';
*
* const logger = new Logger();
*
* class Lambda implements LambdaInterface {
* // Decorate your handler class method
* @logger.injectLambdaContext()
* public async handler(_event: any, _context: any): Promise<void> {
* logger.info('This is an INFO log with some context');
* }
* }
*
* const handlerClass = new Lambda();
* export const handler = handlerClass.handler.bind(handlerClass);
* ```
*
* @see https://www.typescriptlang.org/docs/handbook/decorators.html#method-decorators
* @returns {HandlerMethodDecorator}
*/
public injectLambdaContext(options?: HandlerOptions): HandlerMethodDecorator {
return (_target, _propertyKey, descriptor) => {
/**
* The descriptor.value is the method this decorator decorates, it cannot be undefined.
*/
/* eslint-disable @typescript-eslint/no-non-null-assertion */
const originalMethod = descriptor.value!;
// eslint-disable-next-line @typescript-eslint/no-this-alias
const loggerRef = this;
// Use a function() {} instead of an () => {} arrow function so that we can
// access `myClass` as `this` in a decorated `myClass.myMethod()`.
descriptor.value = (async function (this: Handler, event, context, callback) {
let initialPersistentAttributes = {};
if (options && options.clearState === true) {
initialPersistentAttributes = { ...loggerRef.getPersistentLogAttributes() };
}
Logger.injectLambdaContextBefore(loggerRef, event, context, options);
let result: unknown;
try {
result = await originalMethod.apply(this, [ event, context, callback ]);
} catch (error) {
throw error;
} finally {
Logger.injectLambdaContextAfterOrOnError(loggerRef, initialPersistentAttributes, options);
}
return result;
});
};
}
public static injectLambdaContextAfterOrOnError(logger: Logger, initialPersistentAttributes: LogAttributes, options?: HandlerOptions): void {
if (options && options.clearState === true) {
logger.setPersistentLogAttributes(initialPersistentAttributes);
}
}
public static injectLambdaContextBefore(logger: Logger, event: unknown, context: Context, options?: HandlerOptions): void {
logger.addContext(context);
let shouldLogEvent = undefined;
if (options && options.hasOwnProperty('logEvent')) {
shouldLogEvent = options.logEvent;
}
logger.logEventIfEnabled(event, shouldLogEvent);
}
/**
* Logs a Lambda invocation event, if it *should*.
*
** @param {unknown} event
* @param {boolean} [overwriteValue]
* @returns {void}
*/
public logEventIfEnabled(event: unknown, overwriteValue?: boolean): void {
if (!this.shouldLogEvent(overwriteValue)) {
return;
}
this.info('Lambda invocation event', { event });
}
/**
* If the sample rate feature is enabled, the calculation that determines whether the logs
* will actually be printed or not for this invocation is done when the Logger class is
* initialized.
* This method will repeat that calculation (with possible different outcome).
*
* @returns {void}
*/
public refreshSampleRateCalculation(): void {
this.setLogsSampled();
}
/**
* Alias for removePersistentLogAttributes.
*
* @param {string[]} keys
* @returns {void}
*/
public removeKeys(keys: string[]): void {
this.removePersistentLogAttributes(keys);
}
/**
* It removes attributes based on provided keys to all log items generated by this Logger instance.
*
* @param {string[]} keys
* @returns {void}
*/
public removePersistentLogAttributes(keys: string[]): void {
keys.forEach((key) => {
if (this.persistentLogAttributes && key in this.persistentLogAttributes) {
delete this.persistentLogAttributes[key];
}
});
}
/**
* It sets the given attributes (key-value pairs) to all log items generated by this Logger instance.
* Note: this replaces the pre-existing value.
*
* @param {LogAttributes} attributes
* @returns {void}
*/
public setPersistentLogAttributes(attributes: LogAttributes): void {
this.persistentLogAttributes = attributes;
}
/**
* It sets the user-provided sample rate value.
*
* @param {number} [sampleRateValue]
* @returns {void}
*/
public setSampleRateValue(sampleRateValue?: number): void {
this.powertoolLogData.sampleRateValue =
sampleRateValue ||
this.getCustomConfigService()?.getSampleRateValue() ||
this.getEnvVarsService().getSampleRateValue();
}
/**
* It checks whether the current Lambda invocation event should be printed in the logs or not.
*
* @private
* @param {boolean} [overwriteValue]
* @returns {boolean}
*/
public shouldLogEvent(overwriteValue?: boolean): boolean {
if (typeof overwriteValue === 'boolean') {
return overwriteValue;
}
return this.getLogEvent();
}
/**
* It prints a log item with level WARN.
*
* @param {LogItemMessage} input
* @param {Error | LogAttributes | string} extraInput
* @returns {void}
*/
public warn(input: LogItemMessage, ...extraInput: LogItemExtraInput): void {
this.processLogItem('WARN', input, extraInput);
}
/**
* It stores information that is printed in all log items.
*
* @param {Partial<PowertoolLogData>} attributesArray
* @private
* @returns {void}
*/
private addToPowertoolLogData(...attributesArray: Array<Partial<PowertoolLogData>>): void {
attributesArray.forEach((attributes: Partial<PowertoolLogData>) => {
merge(this.powertoolLogData, attributes);
});
}
/**
* It processes a particular log item so that it can be printed to stdout:
* - Merges ephemeral log attributes with persistent log attributes (printed for all logs) and additional info;
* - Formats all the log attributes;
*
* @private
* @param {LogLevel} logLevel
* @param {LogItemMessage} input
* @param {LogItemExtraInput} extraInput
* @returns {LogItem}
*/
private createAndPopulateLogItem(logLevel: LogLevel, input: LogItemMessage, extraInput: LogItemExtraInput): LogItem {
// TODO: this method's logic is hard to understand, there is an opportunity here to simplify this logic.
const unformattedBaseAttributes = merge({
logLevel,
timestamp: new Date(),
message: typeof input === 'string' ? input : input.message,
xRayTraceId: this.envVarsService.getXrayTraceId(),
}, this.getPowertoolLogData());
const logItem = new LogItem({
baseAttributes: this.getLogFormatter().formatAttributes(unformattedBaseAttributes),
persistentAttributes: this.getPersistentLogAttributes(),
});
// Add ephemeral attributes
if (typeof input !== 'string') {
logItem.addAttributes(input);
}
extraInput.forEach((item: Error | LogAttributes | string) => {
const attributes: LogAttributes =
item instanceof Error ? { error: item } :
typeof item === 'string' ? { extra: item } :
item;
logItem.addAttributes(attributes);
});
return logItem;
}
/**
* It returns the custom config service, an abstraction used to fetch environment variables.
*
* @private
* @returns {ConfigServiceInterface | undefined}
*/
private getCustomConfigService(): ConfigServiceInterface | undefined {
return this.customConfigService;
}
/**
* It returns the instance of a service that fetches environment variables.
*
* @private
* @returns {EnvironmentVariablesService}
*/
private getEnvVarsService(): EnvironmentVariablesService {
return <EnvironmentVariablesService> this.envVarsService;
}
/**
* It returns the instance of a service that formats the structure of a
* log item's keys and values in the desired way.
*
* @private
* @returns {LogFormatterInterface}
*/
private getLogFormatter(): LogFormatterInterface {
return <LogFormatterInterface> this.logFormatter;
}
/**
* It returns the log level set for the Logger instance.
*
* @private
* @returns {LogLevel}
*/
private getLogLevel(): LogLevel {
return <LogLevel> this.logLevel;
}
/**
* It returns information that will be added in all log item by
* this Logger instance (different from user-provided persistent attributes).
*
* @private
* @returns {LogAttributes}
*/
private getPowertoolLogData(): PowertoolLogData {
return this.powertoolLogData;
}
/**
* When the data added in the log item contains object references or BigInt values,
* `JSON.stringify()` can't handle them and instead throws errors:
* `TypeError: cyclic object value` or `TypeError: Do not know how to serialize a BigInt`.
* To mitigate these issues, this method will find and remove all cyclic references and convert BigInt values to strings.
*
* @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify#exceptions
* @private
*/
private getReplacer(): (key: string, value: LogAttributes | Error | bigint) => void {
const references = new WeakSet();
return (key, value) => {
let item = value;
if (item instanceof Error) {
item = this.getLogFormatter().formatError(item);
}
if (typeof item === 'bigint') {
return item.toString();
}
if (typeof item === 'object' && value !== null) {
if (references.has(item)) {
return;
}
references.add(item);
}
return item;
};
}
/**
* It returns the numeric sample rate value.
*
* @private
* @returns {number}
*/
private getSampleRateValue(): number {
if (!this.powertoolLogData.sampleRateValue) {
this.setSampleRateValue();
}
return <number> this.powertoolLogData.sampleRateValue;
}
/**
* It returns true if the provided log level is valid.
*
* @param {LogLevel} logLevel
* @private
* @returns {boolean}
*/
private isValidLogLevel(logLevel?: LogLevel): boolean {
return typeof logLevel === 'string' && logLevel.toUpperCase() in this.logLevelThresholds;
}
/**
* It prints a given log with given log level.
*
* @param {LogLevel} logLevel
* @param {LogItem} log
* @private
*/
private printLog(logLevel: LogLevel, log: LogItem): void {
log.prepareForPrint();
const consoleMethod = logLevel.toLowerCase() as keyof ClassThatLogs;
this.console[consoleMethod](JSON.stringify(log.getAttributes(), this.getReplacer(), this.logIndentation));
}
/**
* It prints a given log with given log level.
*
* @param {LogLevel} logLevel
* @param {LogItem} log
* @private
*/
private processLogItem(logLevel: LogLevel, input: LogItemMessage, extraInput: LogItemExtraInput): void {
if (!this.shouldPrint(logLevel)) {
return;
}
this.printLog(logLevel, this.createAndPopulateLogItem(logLevel, input, extraInput));
}
/**
* It initializes console property as an instance of the internal version of Console() class (PR #748)
* or as the global node console if the `POWERTOOLS_DEV' env variable is set and has truthy value.
*
* @private
* @returns {void}
*/
private setConsole(): void {
if (!this.getEnvVarsService().isDevMode()) {
this.console = new Console({ stdout: process.stdout, stderr: process.stderr });
} else {
this.console = console;
}
}
/**
* Sets the Logger's customer config service instance, which will be used
* to fetch environment variables.
*
* @private
* @param {ConfigServiceInterface} customConfigService
* @returns {void}
*/
private setCustomConfigService(customConfigService?: ConfigServiceInterface): void {
this.customConfigService = customConfigService ? customConfigService : undefined;
}
/**
* Sets the Logger's custom config service instance, which will be used
* to fetch environment variables.
*
* @private
* @param {ConfigServiceInterface} customConfigService
* @returns {void}
*/
private setEnvVarsService(): void {
this.envVarsService = new EnvironmentVariablesService();
}
/**
* If the log event feature is enabled via env variable, it sets a property that tracks whether
* the event passed to the Lambda function handler should be logged or not.
*
* @private
* @returns {void}
*/
private setLogEvent(): void {
if (this.getEnvVarsService().getLogEvent()) {
this.logEvent = true;
}
}
/**
* It sets the log formatter instance, in charge of giving a custom format
* to the structured logs
*
* @private
* @param {LogFormatterInterface} logFormatter
* @returns {void}
*/
private setLogFormatter(logFormatter?: LogFormatterInterface): void {
this.logFormatter = logFormatter || new PowertoolLogFormatter();
}
/**
* If the `POWERTOOLS_DEV' env variable is set,
* it adds JSON indentation for pretty printing logs.
*
* @private
* @returns {void}
*/
private setLogIndentation(): void {
if (this.getEnvVarsService().isDevMode()) {
this.logIndentation = LogJsonIndent.PRETTY;
}
}
/**
* It sets the Logger's instance log level.
*
* @private
* @param {LogLevel} logLevel
* @returns {void}
*/
private setLogLevel(logLevel?: LogLevel): void {
if (this.isValidLogLevel(logLevel)) {
this.logLevel = (<LogLevel>logLevel).toUpperCase();
return;
}
const customConfigValue = this.getCustomConfigService()?.getLogLevel();
if (this.isValidLogLevel(customConfigValue)) {
this.logLevel = (<LogLevel>customConfigValue).toUpperCase();
return;
}
const envVarsValue = this.getEnvVarsService().getLogLevel();
if (this.isValidLogLevel(envVarsValue)) {
this.logLevel = (<LogLevel>envVarsValue).toUpperCase();
return;
}
this.logLevel = Logger.defaultLogLevel;
}
/**
* If the sample rate feature is enabled, it sets a property that tracks whether this Lambda function invocation
* will print logs or not.
*
* @private
* @returns {void}
*/
private setLogsSampled(): void {
const sampleRateValue = this.getSampleRateValue();
// TODO: revisit Math.random() as it's not a real randomization
this.logsSampled = sampleRateValue !== undefined && (sampleRateValue === 1 || Math.random() < sampleRateValue);
}
/**
* It configures the Logger instance settings that will affect the Logger's behaviour
* and the content of all logs.
*
* @private
* @param {ConstructorOptions} options
* @returns {Logger}
*/
private setOptions(options: ConstructorOptions): Logger {
const {
logLevel,
serviceName,
sampleRateValue,
logFormatter,
customConfigService,
persistentLogAttributes,
environment,
} = options;
this.setEnvVarsService();
// order is important, it uses EnvVarsService()
this.setConsole();
this.setCustomConfigService(customConfigService);
this.setLogLevel(logLevel);
this.setSampleRateValue(sampleRateValue);
this.setLogsSampled();
this.setLogFormatter(logFormatter);
this.setPowertoolLogData(serviceName, environment);
this.setLogEvent();
this.setLogIndentation();
this.addPersistentLogAttributes(persistentLogAttributes);
return this;
}
/**
* It adds important data to the Logger instance that will affect the content of all logs.
*
* @param {string} serviceName
* @param {Environment} environment
* @param {LogAttributes} persistentLogAttributes
* @private
* @returns {void}
*/
private setPowertoolLogData(
serviceName?: string,
environment?: Environment,
persistentLogAttributes: LogAttributes = {},
): void {
this.addToPowertoolLogData(
{
awsRegion: this.getEnvVarsService().getAwsRegion(),
environment:
environment ||
this.getCustomConfigService()?.getCurrentEnvironment() ||
this.getEnvVarsService().getCurrentEnvironment(),
sampleRateValue: this.getSampleRateValue(),
serviceName:
serviceName || this.getCustomConfigService()?.getServiceName() || this.getEnvVarsService().getServiceName() || this.getDefaultServiceName(),
},
persistentLogAttributes,
);
}
/**
* It checks whether the current log item should/can be printed.
*
* @param {string} serviceName
* @param {Environment} environment
* @param {LogAttributes} persistentLogAttributes
* @private
* @returns {boolean}
*/
private shouldPrint(logLevel: LogLevel): boolean {
if (this.logLevelThresholds[logLevel] >= this.logLevelThresholds[this.getLogLevel()]) {
return true;
}
return this.getLogsSampled();
}
}
export { Logger };