Skip to content

Commit 650252c

Browse files
authored
feat(logger): introduce loglevel trace #1589 (#2902)
1 parent db0e05c commit 650252c

File tree

5 files changed

+201
-161
lines changed

5 files changed

+201
-161
lines changed

Diff for: docs/core/logger.md

+1
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,7 @@ We support the following log levels:
454454

455455
| Level | Numeric value |
456456
| ---------- | ------------- |
457+
| `TRACE` | 6 |
457458
| `DEBUG` | 8 |
458459
| `INFO` | 12 |
459460
| `WARN` | 16 |

Diff for: packages/logger/src/Logger.ts

+32-8
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ class Logger extends Utility implements LoggerInterface {
158158
* The levels are in ascending order from the most verbose to the least verbose (no logs).
159159
*/
160160
private readonly logLevelThresholds: LogLevelThresholds = {
161+
TRACE: 6,
161162
DEBUG: 8,
162163
INFO: 12,
163164
WARN: 16,
@@ -200,7 +201,7 @@ class Logger extends Utility implements LoggerInterface {
200201
*
201202
* We keep this value to be able to reset the log level to the initial value when the sample rate is refreshed.
202203
*/
203-
#initialLogLevel = 12;
204+
#initialLogLevel = this.logLevelThresholds.INFO;
204205
/**
205206
* Replacer function used to serialize the log items.
206207
*/
@@ -337,7 +338,7 @@ class Logger extends Utility implements LoggerInterface {
337338
input: LogItemMessage,
338339
...extraInput: LogItemExtraInput
339340
): void {
340-
this.processLogItem(24, input, extraInput);
341+
this.processLogItem(this.logLevelThresholds.CRITICAL, input, extraInput);
341342
}
342343

343344
/**
@@ -348,7 +349,7 @@ class Logger extends Utility implements LoggerInterface {
348349
* @returns {void}
349350
*/
350351
public debug(input: LogItemMessage, ...extraInput: LogItemExtraInput): void {
351-
this.processLogItem(8, input, extraInput);
352+
this.processLogItem(this.logLevelThresholds.DEBUG, input, extraInput);
352353
}
353354

354355
/**
@@ -359,7 +360,7 @@ class Logger extends Utility implements LoggerInterface {
359360
* @returns {void}
360361
*/
361362
public error(input: LogItemMessage, ...extraInput: LogItemExtraInput): void {
362-
this.processLogItem(20, input, extraInput);
363+
this.processLogItem(this.logLevelThresholds.ERROR, input, extraInput);
363364
}
364365

365366
/**
@@ -403,7 +404,7 @@ class Logger extends Utility implements LoggerInterface {
403404
* @returns {void}
404405
*/
405406
public info(input: LogItemMessage, ...extraInput: LogItemExtraInput): void {
406-
this.processLogItem(12, input, extraInput);
407+
this.processLogItem(this.logLevelThresholds.INFO, input, extraInput);
407408
}
408409

409410
/**
@@ -636,6 +637,17 @@ class Logger extends Utility implements LoggerInterface {
636637
return this.getLogEvent();
637638
}
638639

640+
/**
641+
* It prints a log item with level TRACE.
642+
*
643+
* @param {LogItemMessage} input
644+
* @param {Error | LogAttributes | string} extraInput
645+
* @returns {void}
646+
*/
647+
public trace(input: LogItemMessage, ...extraInput: LogItemExtraInput): void {
648+
this.processLogItem(this.logLevelThresholds.TRACE, input, extraInput);
649+
}
650+
639651
/**
640652
* It prints a log item with level WARN.
641653
*
@@ -644,7 +656,7 @@ class Logger extends Utility implements LoggerInterface {
644656
* @returns {void}
645657
*/
646658
public warn(input: LogItemMessage, ...extraInput: LogItemExtraInput): void {
647-
this.processLogItem(16, input, extraInput);
659+
this.processLogItem(this.logLevelThresholds.WARN, input, extraInput);
648660
}
649661

650662
/**
@@ -913,7 +925,7 @@ class Logger extends Utility implements LoggerInterface {
913925
log.prepareForPrint();
914926

915927
const consoleMethod =
916-
logLevel === 24
928+
logLevel === this.logLevelThresholds.CRITICAL
917929
? 'error'
918930
: (this.getLogLevelNameFromNumber(logLevel).toLowerCase() as keyof Omit<
919931
LogFunction,
@@ -970,6 +982,13 @@ class Logger extends Utility implements LoggerInterface {
970982
} else {
971983
this.console = console;
972984
}
985+
986+
/**
987+
* Patch `console.trace` to avoid printing a stack trace and aligning with AWS Lambda behavior - see #2902
988+
*/
989+
this.console.trace = (message: string, ...optionalParams: unknown[]) => {
990+
this.console.log(message, ...optionalParams);
991+
};
973992
}
974993

975994
/**
@@ -1050,7 +1069,12 @@ class Logger extends Utility implements LoggerInterface {
10501069
if (this.isValidSampleRate(value)) {
10511070
this.powertoolsLogData.sampleRateValue = value;
10521071

1053-
if (value && randomInt(0, 100) / 100 <= value) {
1072+
if (
1073+
this.logLevel > this.logLevelThresholds.DEBUG &&
1074+
value &&
1075+
randomInt(0, 100) / 100 <= value
1076+
) {
1077+
// only change logLevel if higher than debug, i.e. don't change from e.g. tracing to debug
10541078
this.setLogLevel('DEBUG');
10551079
this.debug('Setting log level to DEBUG due to sampling rate');
10561080
} else {

Diff for: packages/logger/src/constants.ts

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ const LogJsonIndent = {
1515
} as const;
1616

1717
const LogLevel = {
18+
TRACE: 'TRACE',
1819
DEBUG: 'DEBUG',
1920
INFO: 'INFO',
2021
WARN: 'WARN',

Diff for: packages/logger/src/types/Logger.ts

+1
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ type LoggerInterface = {
148148
setLogLevel(logLevel: LogLevel): void;
149149
setPersistentLogAttributes(attributes?: LogAttributes): void;
150150
shouldLogEvent(overwriteValue?: boolean): boolean;
151+
trace(input: LogItemMessage, ...extraInput: LogItemExtraInput): void;
151152
warn(input: LogItemMessage, ...extraInput: LogItemExtraInput): void;
152153
};
153154

0 commit comments

Comments
 (0)