Skip to content

Commit 69d52f2

Browse files
authored
Merge branch 'main' into docs/logger_logEvent
2 parents 66bec07 + 650252c commit 69d52f2

File tree

7 files changed

+205
-165
lines changed

7 files changed

+205
-165
lines changed

.github/workflows/ossf_scorecard.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,6 @@ jobs:
4343

4444
# Upload the results to GitHub's code scanning dashboard.
4545
- name: "Upload to code-scanning"
46-
uses: github/codeql-action/upload-sarif@eb055d739abdc2e8de2e5f4ba1a8b246daa779aa # v3.26.0
46+
uses: github/codeql-action/upload-sarif@29d86d22a34ea372b1bbf3b2dced2e25ca6b3384 # v3.26.1
4747
with:
4848
sarif_file: results.sarif

docs/core/logger.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -476,6 +476,7 @@ We support the following log levels:
476476

477477
| Level | Numeric value |
478478
| ---------- | ------------- |
479+
| `TRACE` | 6 |
479480
| `DEBUG` | 8 |
480481
| `INFO` | 12 |
481482
| `WARN` | 16 |

package-lock.json

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/logger/src/Logger.ts

Lines changed: 32 additions & 8 deletions
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
/**
@@ -649,6 +650,17 @@ class Logger extends Utility implements LoggerInterface {
649650
return this.getLogEvent();
650651
}
651652

653+
/**
654+
* It prints a log item with level TRACE.
655+
*
656+
* @param {LogItemMessage} input
657+
* @param {Error | LogAttributes | string} extraInput
658+
* @returns {void}
659+
*/
660+
public trace(input: LogItemMessage, ...extraInput: LogItemExtraInput): void {
661+
this.processLogItem(this.logLevelThresholds.TRACE, input, extraInput);
662+
}
663+
652664
/**
653665
* It prints a log item with level WARN.
654666
*
@@ -657,7 +669,7 @@ class Logger extends Utility implements LoggerInterface {
657669
* @returns {void}
658670
*/
659671
public warn(input: LogItemMessage, ...extraInput: LogItemExtraInput): void {
660-
this.processLogItem(16, input, extraInput);
672+
this.processLogItem(this.logLevelThresholds.WARN, input, extraInput);
661673
}
662674

663675
/**
@@ -926,7 +938,7 @@ class Logger extends Utility implements LoggerInterface {
926938
log.prepareForPrint();
927939

928940
const consoleMethod =
929-
logLevel === 24
941+
logLevel === this.logLevelThresholds.CRITICAL
930942
? 'error'
931943
: (this.getLogLevelNameFromNumber(logLevel).toLowerCase() as keyof Omit<
932944
LogFunction,
@@ -983,6 +995,13 @@ class Logger extends Utility implements LoggerInterface {
983995
} else {
984996
this.console = console;
985997
}
998+
999+
/**
1000+
* Patch `console.trace` to avoid printing a stack trace and aligning with AWS Lambda behavior - see #2902
1001+
*/
1002+
this.console.trace = (message: string, ...optionalParams: unknown[]) => {
1003+
this.console.log(message, ...optionalParams);
1004+
};
9861005
}
9871006

9881007
/**
@@ -1063,7 +1082,12 @@ class Logger extends Utility implements LoggerInterface {
10631082
if (this.isValidSampleRate(value)) {
10641083
this.powertoolsLogData.sampleRateValue = value;
10651084

1066-
if (value && randomInt(0, 100) / 100 <= value) {
1085+
if (
1086+
this.logLevel > this.logLevelThresholds.DEBUG &&
1087+
value &&
1088+
randomInt(0, 100) / 100 <= value
1089+
) {
1090+
// only change logLevel if higher than debug, i.e. don't change from e.g. tracing to debug
10671091
this.setLogLevel('DEBUG');
10681092
this.debug('Setting log level to DEBUG due to sampling rate');
10691093
} else {

packages/logger/src/constants.ts

Lines changed: 1 addition & 0 deletions
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',

packages/logger/src/types/Logger.ts

Lines changed: 1 addition & 0 deletions
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)