@@ -176,25 +176,35 @@ class Logger extends Utility implements LoggerInterface {
176
176
#jsonReplacerFn?: CustomJsonReplacerFn ;
177
177
178
178
/**
179
- * Represents whether the buffering functionality is enabled in the logger
179
+ * Buffer configuration options.
180
180
*/
181
- protected isBufferEnabled = false ;
182
-
183
- /**
184
- * Whether the buffer should be flushed when an error is logged
185
- */
186
- protected flushOnErrorLog = true ;
187
- /**
188
- * Log level threshold for the buffer
189
- * Logs with a level lower than this threshold will be buffered
190
- * Default is DEBUG
191
- */
192
- protected bufferAtVerbosity : number = LogLevelThreshold . DEBUG ;
193
- /**
194
- * Max size of the buffer. Additions to the buffer beyond this size will
195
- * cause older logs to be evicted from the buffer
196
- */
197
- #maxBufferBytesSize = 20480 ;
181
+ #bufferOptions: {
182
+ /**
183
+ * Whether the buffer should is enabled
184
+ */
185
+ enabled : boolean ;
186
+ /**
187
+ * Whether the buffer should be flushed when an error is logged
188
+ */
189
+ flushOnErrorLog : boolean ;
190
+ /**
191
+ * Max size of the buffer. Additions to the buffer beyond this size will
192
+ * cause older logs to be evicted from the buffer
193
+ */
194
+ maxBytes : number ;
195
+ /**
196
+ * Log level threshold for the buffer
197
+ * Logs with a level lower than this threshold will be buffered
198
+ * Default is DEBUG
199
+ * Can be specified as a number (LogLevelThreshold value) or a string (log level name)
200
+ */
201
+ bufferAtVerbosity : number ;
202
+ } = {
203
+ enabled : false ,
204
+ flushOnErrorLog : true ,
205
+ maxBytes : 20480 ,
206
+ bufferAtVerbosity : LogLevelThreshold . DEBUG ,
207
+ } ;
198
208
199
209
/**
200
210
* Contains buffered logs, grouped by `_X_AMZN_TRACE_ID`, each group with a max size of `maxBufferBytesSize`
@@ -295,8 +305,16 @@ class Logger extends Utility implements LoggerInterface {
295
305
customConfigService : this . getCustomConfigService ( ) ,
296
306
environment : this . powertoolsLogData . environment ,
297
307
persistentLogAttributes : this . persistentLogAttributes ,
298
- temporaryLogAttributes : this . temporaryLogAttributes ,
299
308
jsonReplacerFn : this . #jsonReplacerFn,
309
+ ...( this . #bufferOptions. enabled && {
310
+ logBufferOptions : {
311
+ maxBytes : this . #bufferOptions. maxBytes ,
312
+ bufferAtVerbosity : this . getLogLevelNameFromNumber (
313
+ this . #bufferOptions. bufferAtVerbosity
314
+ ) ,
315
+ flushOnErrorLog : this . #bufferOptions. flushOnErrorLog ,
316
+ } ,
317
+ } ) ,
300
318
} ,
301
319
options
302
320
)
@@ -305,6 +323,9 @@ class Logger extends Utility implements LoggerInterface {
305
323
childLogger . addContext (
306
324
this . powertoolsLogData . lambdaContext as unknown as Context
307
325
) ;
326
+ if ( this . temporaryLogAttributes ) {
327
+ childLogger . appendKeys ( this . temporaryLogAttributes ) ;
328
+ }
308
329
309
330
return childLogger ;
310
331
}
@@ -339,7 +360,7 @@ class Logger extends Utility implements LoggerInterface {
339
360
* @param extraInput - The extra input to log.
340
361
*/
341
362
public error ( input : LogItemMessage , ...extraInput : LogItemExtraInput ) : void {
342
- if ( this . isBufferEnabled && this . flushOnErrorLog ) {
363
+ if ( this . #bufferOptions . enabled && this . #bufferOptions . flushOnErrorLog ) {
343
364
this . flushBuffer ( ) ;
344
365
}
345
366
this . processLogItem ( LogLevelThreshold . ERROR , input , extraInput ) ;
@@ -1273,24 +1294,25 @@ class Logger extends Utility implements LoggerInterface {
1273
1294
return ;
1274
1295
}
1275
1296
// `enabled` is a boolean, so we set it to true if it's not explicitly set to false
1276
- this . isBufferEnabled = options ?. enabled !== false ;
1297
+ this . #bufferOptions . enabled = options ?. enabled !== false ;
1277
1298
// if `enabled` is false, we don't need to set any other options
1278
- if ( this . isBufferEnabled === false ) return ;
1299
+ if ( this . #bufferOptions . enabled === false ) return ;
1279
1300
1280
1301
if ( options ?. maxBytes !== undefined ) {
1281
- this . #maxBufferBytesSize = options . maxBytes ;
1302
+ this . #bufferOptions . maxBytes = options . maxBytes ;
1282
1303
}
1283
1304
this . #buffer = new CircularMap ( {
1284
- maxBytesSize : this . #maxBufferBytesSize ,
1305
+ maxBytesSize : this . #bufferOptions . maxBytes ,
1285
1306
} ) ;
1286
1307
1287
1308
if ( options ?. flushOnErrorLog === false ) {
1288
- this . flushOnErrorLog = false ;
1309
+ this . #bufferOptions . flushOnErrorLog = false ;
1289
1310
}
1290
1311
1291
1312
const bufferAtLogLevel = options ?. bufferAtVerbosity ?. toUpperCase ( ) ;
1292
1313
if ( this . isValidLogLevel ( bufferAtLogLevel ) ) {
1293
- this . bufferAtVerbosity = LogLevelThreshold [ bufferAtLogLevel ] ;
1314
+ this . #bufferOptions. bufferAtVerbosity =
1315
+ LogLevelThreshold [ bufferAtLogLevel ] ;
1294
1316
}
1295
1317
}
1296
1318
@@ -1378,9 +1400,9 @@ class Logger extends Utility implements LoggerInterface {
1378
1400
logLevel : number
1379
1401
) : boolean {
1380
1402
return (
1381
- this . isBufferEnabled &&
1403
+ this . #bufferOptions . enabled &&
1382
1404
traceId !== undefined &&
1383
- logLevel <= this . bufferAtVerbosity
1405
+ logLevel <= this . #bufferOptions . bufferAtVerbosity
1384
1406
) ;
1385
1407
}
1386
1408
}
0 commit comments