@@ -172,25 +172,28 @@ class Logger extends Utility implements LoggerInterface {
172
172
/**
173
173
* Represents whether the buffering functionality is enabled in the logger
174
174
*/
175
- protected isBufferEnabled = false ;
175
+ protected isBufferEnabled = true ;
176
176
177
+ /**
178
+ * Whether the buffer should be flushed when an error is logged
179
+ */
180
+ protected flushOnErrorLog = true ;
177
181
/**
178
182
* Log level threshold for the buffer
179
183
* Logs with a level lower than this threshold will be buffered
184
+ * Default is DEBUG
180
185
*/
181
- protected bufferLogThreshold : number = LogLevelThreshold . DEBUG ;
186
+ protected bufferAtVerbosity : number = LogLevelThreshold . DEBUG ;
182
187
/**
183
188
* Max size of the buffer. Additions to the buffer beyond this size will
184
189
* cause older logs to be evicted from the buffer
185
190
*/
186
- readonly #maxBufferBytesSize = 1024 ;
191
+ #maxBufferBytesSize = 1024 ;
187
192
188
193
/**
189
194
* Contains buffered logs, grouped by _X_AMZN_TRACE_ID, each group with a max size of `maxBufferBytesSize`
190
195
*/
191
- readonly #buffer: CircularMap < string > = new CircularMap ( {
192
- maxBytesSize : this . #maxBufferBytesSize,
193
- } ) ;
196
+ #buffer?: CircularMap < string > ;
194
197
195
198
/**
196
199
* Log level used by the current instance of Logger.
@@ -330,6 +333,9 @@ class Logger extends Utility implements LoggerInterface {
330
333
* @param extraInput - The extra input to log.
331
334
*/
332
335
public error ( input : LogItemMessage , ...extraInput : LogItemExtraInput ) : void {
336
+ if ( this . isBufferEnabled && this . flushOnErrorLog ) {
337
+ this . flushBuffer ( ) ;
338
+ }
333
339
this . processLogItem ( LogLevelThreshold . ERROR , input , extraInput ) ;
334
340
}
335
341
@@ -1168,6 +1174,7 @@ class Logger extends Utility implements LoggerInterface {
1168
1174
environment,
1169
1175
jsonReplacerFn,
1170
1176
logRecordOrder,
1177
+ logBufferOptions,
1171
1178
} = options ;
1172
1179
1173
1180
if ( persistentLogAttributes && persistentKeys ) {
@@ -1194,6 +1201,10 @@ class Logger extends Utility implements LoggerInterface {
1194
1201
this . setLogIndentation ( ) ;
1195
1202
this . #jsonReplacerFn = jsonReplacerFn ;
1196
1203
1204
+ if ( logBufferOptions !== undefined ) {
1205
+ this . #setLogBuffering( logBufferOptions ) ;
1206
+ }
1207
+
1197
1208
return this ;
1198
1209
}
1199
1210
@@ -1224,6 +1235,30 @@ class Logger extends Utility implements LoggerInterface {
1224
1235
persistentKeys && this . appendPersistentKeys ( persistentKeys ) ;
1225
1236
}
1226
1237
1238
+ #setLogBuffering(
1239
+ options : NonNullable < ConstructorOptions [ 'logBufferOptions' ] >
1240
+ ) {
1241
+ if ( options . maxBytes !== undefined ) {
1242
+ this . #maxBufferBytesSize = options . maxBytes ;
1243
+ }
1244
+
1245
+ this . #buffer = new CircularMap ( {
1246
+ maxBytesSize : this . #maxBufferBytesSize,
1247
+ } ) ;
1248
+
1249
+ if ( options . enabled !== undefined ) {
1250
+ this . isBufferEnabled = options . enabled ;
1251
+ }
1252
+
1253
+ const bufferAtLogLevel = options . bufferAtVerbosity ?. toUpperCase ( ) ;
1254
+
1255
+ if ( this . isValidLogLevel ( bufferAtLogLevel ) ) {
1256
+ this . bufferAtVerbosity = LogLevelThreshold [ bufferAtLogLevel ] ;
1257
+
1258
+ return ;
1259
+ }
1260
+ }
1261
+
1227
1262
/**
1228
1263
* Add a log to the buffer
1229
1264
* @param xrayTraceId - _X_AMZN_TRACE_ID of the request
@@ -1243,20 +1278,20 @@ class Logger extends Utility implements LoggerInterface {
1243
1278
this . logIndentation
1244
1279
) ;
1245
1280
1246
- this . #buffer. setItem ( xrayTraceId , stringified , logLevel ) ;
1281
+ this . #buffer? .setItem ( xrayTraceId , stringified , logLevel ) ;
1247
1282
}
1248
1283
1249
1284
/**
1250
1285
* Flushes all items of the respective _X_AMZN_TRACE_ID within
1251
1286
* the buffer.
1252
1287
*/
1253
- protected flushBuffer ( ) : void {
1288
+ flushBuffer ( ) : void {
1254
1289
const traceId = this . envVarsService . getXrayTraceId ( ) ;
1255
1290
if ( traceId === undefined ) {
1256
1291
return ;
1257
1292
}
1258
1293
1259
- const buffer = this . #buffer. get ( traceId ) ;
1294
+ const buffer = this . #buffer? .get ( traceId ) ;
1260
1295
if ( buffer === undefined ) {
1261
1296
return ;
1262
1297
}
@@ -1281,7 +1316,7 @@ class Logger extends Utility implements LoggerInterface {
1281
1316
) ;
1282
1317
}
1283
1318
1284
- this . #buffer. delete ( traceId ) ;
1319
+ this . #buffer? .delete ( traceId ) ;
1285
1320
}
1286
1321
/**
1287
1322
* Tests if the log meets the criteria to be buffered
@@ -1295,7 +1330,7 @@ class Logger extends Utility implements LoggerInterface {
1295
1330
return (
1296
1331
this . isBufferEnabled &&
1297
1332
traceId !== undefined &&
1298
- logLevel <= this . bufferLogThreshold
1333
+ logLevel <= this . bufferAtVerbosity
1299
1334
) ;
1300
1335
}
1301
1336
}
0 commit comments