@@ -81,14 +81,11 @@ class Logger implements ClassThatLogs {
81
81
}
82
82
83
83
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 ) ;
88
85
}
89
86
90
87
public error ( input : LogItemMessage , ...extraInput : LogItemExtraInput ) : void {
91
- this . printLog ( this . createAndPopulateLogItem ( 'ERROR' , input , extraInput ) ) ;
88
+ this . processLogItem ( 'ERROR' , input , extraInput ) ;
92
89
}
93
90
94
91
public static evaluateColdStartOnce ( ) : void {
@@ -110,10 +107,7 @@ class Logger implements ClassThatLogs {
110
107
}
111
108
112
109
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 ) ;
117
111
}
118
112
119
113
public injectLambdaContext ( ) : HandlerMethodDecorator {
@@ -149,10 +143,7 @@ class Logger implements ClassThatLogs {
149
143
}
150
144
151
145
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 ) ;
156
147
}
157
148
158
149
private addToPowertoolLogData ( ...attributesArray : Array < Partial < PowertoolLogData > > ) : void {
@@ -234,29 +225,46 @@ class Logger implements ClassThatLogs {
234
225
return typeof logLevel === 'string' && logLevel . toUpperCase ( ) in this . logLevelThresholds ;
235
226
}
236
227
237
- private printLog ( log : LogItem ) : void {
228
+ private printLog ( logLevel : LogLevel , log : LogItem ) : void {
238
229
log . prepareForPrint ( ) ;
239
230
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 {
240
252
const references = new WeakSet ( ) ;
241
253
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
+ } ;
260
268
}
261
269
262
270
private setCustomConfigService ( customConfigService ?: ConfigServiceInterface ) : void {
0 commit comments