Skip to content

Commit 7e99f1f

Browse files
committed
feat(logger): save formatted log to context
1 parent 45a67a6 commit 7e99f1f

File tree

1 file changed

+24
-20
lines changed

1 file changed

+24
-20
lines changed

packages/logger/src/Logger.ts

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -140,9 +140,7 @@ class Logger extends Utility implements LoggerInterface {
140140
*/
141141
#buffer: [number, Parameters<Logger['createAndPopulateLogItem']>][] = [];
142142

143-
144-
#context: Record<string, Array<Parameters<Logger['createAndPopulateLogItem']>>> = {}
145-
143+
#context: Record<string, Array<[number, string]>> = {};
146144

147145
/**
148146
* Flag used to determine if the logger is initialized.
@@ -841,9 +839,7 @@ class Logger extends Utility implements LoggerInterface {
841839
* @param logLevel - The log level
842840
* @param log - The log item to print
843841
*/
844-
private printLog(logLevel: number, log: LogItem): void {
845-
log.prepareForPrint();
846-
842+
private printLog(logLevel: number, log: LogItem | string): void {
847843
const consoleMethod =
848844
logLevel === LogLevelThreshold.CRITICAL
849845
? 'error'
@@ -853,11 +849,17 @@ class Logger extends Utility implements LoggerInterface {
853849
>);
854850

855851
this.console[consoleMethod](
856-
JSON.stringify(
857-
log.getAttributes(),
858-
this.getJsonReplacer(),
859-
this.logIndentation
860-
)
852+
typeof log === 'string' ? log : this.formatLog(log)
853+
);
854+
}
855+
856+
private formatLog(log: LogItem): string {
857+
log.prepareForPrint();
858+
859+
return JSON.stringify(
860+
log.getAttributes(),
861+
this.getJsonReplacer(),
862+
this.logIndentation
861863
);
862864
}
863865

@@ -874,17 +876,14 @@ class Logger extends Utility implements LoggerInterface {
874876
extraInput: LogItemExtraInput
875877
): void {
876878
if (logLevel >= this.logLevel) {
877-
const xRayTraceId = this.envVarsService.getXrayTraceId();
878-
879+
const xRayTraceId = this.envVarsService.getXrayTraceId() as string;
880+
879881
// Print all log items in the context
880882
if (this.#context[xRayTraceId]) {
881883
for (const contextItem of this.#context[xRayTraceId]) {
882-
this.printLog(
883-
logLevel,
884-
this.createAndPopulateLogItem(...contextItem)
885-
);
884+
this.printLog(...contextItem);
886885
}
887-
886+
888887
// Clear the context after flushing
889888
// This also removes entries from other X-Ray trace IDs
890889
this.#context = {};
@@ -903,9 +902,14 @@ class Logger extends Utility implements LoggerInterface {
903902

904903
// Add the log item to the context
905904
const context = this.#context[xRayTraceId] ?? [];
906-
context.push([logLevel, input, extraInput]);
905+
context.push([
906+
logLevel,
907+
this.formatLog(
908+
this.createAndPopulateLogItem(logLevel, input, extraInput)
909+
),
910+
]);
907911

908-
// Assign the updated context to the context property
912+
// Assign the updated context to the context property
909913
// This also removes other X-Ray trace IDs from the context
910914
this.#context = {
911915
[xRayTraceId]: context,

0 commit comments

Comments
 (0)