@@ -174,23 +174,26 @@ class Logger extends Utility implements LoggerInterface {
174
174
*/
175
175
protected isBufferEnabled = false ;
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 = 20480 ;
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
@@ -1167,6 +1173,7 @@ class Logger extends Utility implements LoggerInterface {
1167
1173
environment,
1168
1174
jsonReplacerFn,
1169
1175
logRecordOrder,
1176
+ logBufferOptions,
1170
1177
} = options ;
1171
1178
1172
1179
if ( persistentLogAttributes && persistentKeys ) {
@@ -1193,6 +1200,10 @@ class Logger extends Utility implements LoggerInterface {
1193
1200
this . setLogIndentation ( ) ;
1194
1201
this . #jsonReplacerFn = jsonReplacerFn ;
1195
1202
1203
+ if ( logBufferOptions !== undefined ) {
1204
+ this . #setLogBuffering( logBufferOptions ) ;
1205
+ }
1206
+
1196
1207
return this ;
1197
1208
}
1198
1209
@@ -1223,6 +1234,35 @@ class Logger extends Utility implements LoggerInterface {
1223
1234
persistentKeys && this . appendPersistentKeys ( persistentKeys ) ;
1224
1235
}
1225
1236
1237
+ #setLogBuffering(
1238
+ options : NonNullable < ConstructorOptions [ 'logBufferOptions' ] >
1239
+ ) {
1240
+ if ( options . maxBytes !== undefined ) {
1241
+ this . #maxBufferBytesSize = options . maxBytes ;
1242
+ }
1243
+
1244
+ this . #buffer = new CircularMap ( {
1245
+ maxBytesSize : this . #maxBufferBytesSize,
1246
+ } ) ;
1247
+
1248
+ if ( options . enabled === false ) {
1249
+ this . isBufferEnabled = false ;
1250
+ } else {
1251
+ this . isBufferEnabled = true ;
1252
+ }
1253
+
1254
+ if ( options . flushOnErrorLog === false ) {
1255
+ this . flushOnErrorLog = false ;
1256
+ } else {
1257
+ this . flushOnErrorLog = true ;
1258
+ }
1259
+ const bufferAtLogLevel = options . bufferAtVerbosity ?. toUpperCase ( ) ;
1260
+
1261
+ if ( this . isValidLogLevel ( bufferAtLogLevel ) ) {
1262
+ this . bufferAtVerbosity = LogLevelThreshold [ bufferAtLogLevel ] ;
1263
+ }
1264
+ }
1265
+
1226
1266
/**
1227
1267
* Add a log to the buffer
1228
1268
* @param xrayTraceId - _X_AMZN_TRACE_ID of the request
@@ -1242,20 +1282,20 @@ class Logger extends Utility implements LoggerInterface {
1242
1282
this . logIndentation
1243
1283
) ;
1244
1284
1245
- this . #buffer. setItem ( xrayTraceId , stringified , logLevel ) ;
1285
+ this . #buffer? .setItem ( xrayTraceId , stringified , logLevel ) ;
1246
1286
}
1247
1287
1248
1288
/**
1249
1289
* Flushes all items of the respective _X_AMZN_TRACE_ID within
1250
1290
* the buffer.
1251
1291
*/
1252
- protected flushBuffer ( ) : void {
1292
+ public flushBuffer ( ) : void {
1253
1293
const traceId = this . envVarsService . getXrayTraceId ( ) ;
1254
1294
if ( traceId === undefined ) {
1255
1295
return ;
1256
1296
}
1257
1297
1258
- const buffer = this . #buffer. get ( traceId ) ;
1298
+ const buffer = this . #buffer? .get ( traceId ) ;
1259
1299
if ( buffer === undefined ) {
1260
1300
return ;
1261
1301
}
@@ -1280,7 +1320,7 @@ class Logger extends Utility implements LoggerInterface {
1280
1320
) ;
1281
1321
}
1282
1322
1283
- this . #buffer. delete ( traceId ) ;
1323
+ this . #buffer? .delete ( traceId ) ;
1284
1324
}
1285
1325
/**
1286
1326
* Tests if the log meets the criteria to be buffered
@@ -1294,7 +1334,7 @@ class Logger extends Utility implements LoggerInterface {
1294
1334
return (
1295
1335
this . isBufferEnabled &&
1296
1336
traceId !== undefined &&
1297
- logLevel <= this . bufferLogThreshold
1337
+ logLevel <= this . bufferAtVerbosity
1298
1338
) ;
1299
1339
}
1300
1340
}
0 commit comments