Skip to content

Commit 23ee7f4

Browse files
authored
fix(logger): correct log level in cloudwatch (#386)
1 parent b8ee843 commit 23ee7f4

File tree

3 files changed

+166
-134
lines changed

3 files changed

+166
-134
lines changed

packages/logger/src/Logger.ts

+40-32
Original file line numberDiff line numberDiff line change
@@ -81,14 +81,11 @@ class Logger implements ClassThatLogs {
8181
}
8282

8383
public debug(input: LogItemMessage, ...extraInput: LogItemExtraInput): void {
84-
if (!this.shouldPrint('DEBUG')) {
85-
return;
86-
}
87-
this.printLog(this.createAndPopulateLogItem('DEBUG', input, extraInput));
84+
this.processLogItem('DEBUG', input, extraInput);
8885
}
8986

9087
public error(input: LogItemMessage, ...extraInput: LogItemExtraInput): void {
91-
this.printLog(this.createAndPopulateLogItem('ERROR', input, extraInput));
88+
this.processLogItem('ERROR', input, extraInput);
9289
}
9390

9491
public static evaluateColdStartOnce(): void {
@@ -110,10 +107,7 @@ class Logger implements ClassThatLogs {
110107
}
111108

112109
public info(input: LogItemMessage, ...extraInput: LogItemExtraInput): void {
113-
if (!this.shouldPrint('INFO')) {
114-
return;
115-
}
116-
this.printLog(this.createAndPopulateLogItem('INFO', input, extraInput));
110+
this.processLogItem('INFO', input, extraInput);
117111
}
118112

119113
public injectLambdaContext(): HandlerMethodDecorator {
@@ -149,10 +143,7 @@ class Logger implements ClassThatLogs {
149143
}
150144

151145
public warn(input: LogItemMessage, ...extraInput: LogItemExtraInput): void {
152-
if (!this.shouldPrint('WARN')) {
153-
return;
154-
}
155-
this.printLog(this.createAndPopulateLogItem('WARN', input, extraInput));
146+
this.processLogItem('WARN', input, extraInput);
156147
}
157148

158149
private addToPowertoolLogData(...attributesArray: Array<Partial<PowertoolLogData>>): void {
@@ -234,29 +225,46 @@ class Logger implements ClassThatLogs {
234225
return typeof logLevel === 'string' && logLevel.toUpperCase() in this.logLevelThresholds;
235226
}
236227

237-
private printLog(log: LogItem): void {
228+
private printLog(logLevel: LogLevel, log: LogItem): void {
238229
log.prepareForPrint();
239230

231+
const consoleMethod = logLevel.toLowerCase() as keyof ClassThatLogs;
232+
233+
console[consoleMethod](JSON.stringify(log.getAttributes(), this.removeCircularDependencies()));
234+
}
235+
236+
private processLogItem(logLevel: LogLevel, input: LogItemMessage, extraInput: LogItemExtraInput): void {
237+
if (!this.shouldPrint(logLevel)) {
238+
return;
239+
}
240+
this.printLog(logLevel, this.createAndPopulateLogItem(logLevel, input, extraInput));
241+
}
242+
243+
/**
244+
* When the data added in the log item when contains object references,
245+
* JSON.stringify() doesn't try to solve them and throws an error: TypeError: cyclic object value.
246+
* To mitigate this issue, this function will find and remove the cyclic references.
247+
*
248+
* @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/Cyclic_object_value
249+
* @private
250+
*/
251+
private removeCircularDependencies(): (key: string, value: LogAttributes) => void {
240252
const references = new WeakSet();
241253

242-
console.log(
243-
JSON.parse(
244-
JSON.stringify(log.getAttributes(), (key: string, value: LogAttributes) => {
245-
let item = value;
246-
if (item instanceof Error) {
247-
item = this.getLogFormatter().formatError(item);
248-
}
249-
if (typeof item === 'object' && value !== null) {
250-
if (references.has(item)) {
251-
return;
252-
}
253-
references.add(item);
254-
}
255-
256-
return item;
257-
}),
258-
),
259-
);
254+
return (key, value) => {
255+
let item = value;
256+
if (item instanceof Error) {
257+
item = this.getLogFormatter().formatError(item);
258+
}
259+
if (typeof item === 'object' && value !== null) {
260+
if (references.has(item)) {
261+
return;
262+
}
263+
references.add(item);
264+
}
265+
266+
return item;
267+
};
260268
}
261269

262270
private setCustomConfigService(customConfigService?: ConfigServiceInterface): void {

packages/logger/src/formatter/PowertoolLogFormatter.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ class PowertoolLogFormatter extends LogFormatter {
99
cold_start: attributes.lambdaContext?.coldStart,
1010
function_arn: attributes.lambdaContext?.invokedFunctionArn,
1111
function_memory_size: attributes.lambdaContext?.memoryLimitInMB,
12-
function_request_id: attributes.lambdaContext?.awsRequestId,
1312
function_name: attributes.lambdaContext?.functionName,
13+
function_request_id: attributes.lambdaContext?.awsRequestId,
1414
level: attributes.logLevel,
1515
message: attributes.message,
1616
sampling_rate: attributes.sampleRateValue,

0 commit comments

Comments
 (0)