Skip to content

Commit 0407074

Browse files
committed
chore: propagate buffer config to child loggers
1 parent 969cdfc commit 0407074

File tree

2 files changed

+67
-28
lines changed

2 files changed

+67
-28
lines changed

packages/logger/src/Logger.ts

+50-28
Original file line numberDiff line numberDiff line change
@@ -176,25 +176,35 @@ class Logger extends Utility implements LoggerInterface {
176176
#jsonReplacerFn?: CustomJsonReplacerFn;
177177

178178
/**
179-
* Represents whether the buffering functionality is enabled in the logger
179+
* Buffer configuration options.
180180
*/
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+
};
198208

199209
/**
200210
* 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 {
295305
customConfigService: this.getCustomConfigService(),
296306
environment: this.powertoolsLogData.environment,
297307
persistentLogAttributes: this.persistentLogAttributes,
298-
temporaryLogAttributes: this.temporaryLogAttributes,
299308
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+
}),
300318
},
301319
options
302320
)
@@ -305,6 +323,9 @@ class Logger extends Utility implements LoggerInterface {
305323
childLogger.addContext(
306324
this.powertoolsLogData.lambdaContext as unknown as Context
307325
);
326+
if (this.temporaryLogAttributes) {
327+
childLogger.appendKeys(this.temporaryLogAttributes);
328+
}
308329

309330
return childLogger;
310331
}
@@ -339,7 +360,7 @@ class Logger extends Utility implements LoggerInterface {
339360
* @param extraInput - The extra input to log.
340361
*/
341362
public error(input: LogItemMessage, ...extraInput: LogItemExtraInput): void {
342-
if (this.isBufferEnabled && this.flushOnErrorLog) {
363+
if (this.#bufferOptions.enabled && this.#bufferOptions.flushOnErrorLog) {
343364
this.flushBuffer();
344365
}
345366
this.processLogItem(LogLevelThreshold.ERROR, input, extraInput);
@@ -1273,24 +1294,25 @@ class Logger extends Utility implements LoggerInterface {
12731294
return;
12741295
}
12751296
// `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;
12771298
// 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;
12791300

12801301
if (options?.maxBytes !== undefined) {
1281-
this.#maxBufferBytesSize = options.maxBytes;
1302+
this.#bufferOptions.maxBytes = options.maxBytes;
12821303
}
12831304
this.#buffer = new CircularMap({
1284-
maxBytesSize: this.#maxBufferBytesSize,
1305+
maxBytesSize: this.#bufferOptions.maxBytes,
12851306
});
12861307

12871308
if (options?.flushOnErrorLog === false) {
1288-
this.flushOnErrorLog = false;
1309+
this.#bufferOptions.flushOnErrorLog = false;
12891310
}
12901311

12911312
const bufferAtLogLevel = options?.bufferAtVerbosity?.toUpperCase();
12921313
if (this.isValidLogLevel(bufferAtLogLevel)) {
1293-
this.bufferAtVerbosity = LogLevelThreshold[bufferAtLogLevel];
1314+
this.#bufferOptions.bufferAtVerbosity =
1315+
LogLevelThreshold[bufferAtLogLevel];
12941316
}
12951317
}
12961318

@@ -1378,9 +1400,9 @@ class Logger extends Utility implements LoggerInterface {
13781400
logLevel: number
13791401
): boolean {
13801402
return (
1381-
this.isBufferEnabled &&
1403+
this.#bufferOptions.enabled &&
13821404
traceId !== undefined &&
1383-
logLevel <= this.bufferAtVerbosity
1405+
logLevel <= this.#bufferOptions.bufferAtVerbosity
13841406
);
13851407
}
13861408
}

packages/logger/tests/unit/logBuffer.test.ts

+17
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,23 @@ describe('Buffer logs', () => {
208208
expect(console.error).toBeCalledTimes(1);
209209
});
210210

211+
it('passes down the same buffer config to child loggers', () => {
212+
// Prepare
213+
const logger = new Logger({
214+
logLevel: LogLevel.TRACE,
215+
logBufferOptions: { enabled: true, bufferAtVerbosity: LogLevel.INFO },
216+
});
217+
const childLogger = logger.createChild();
218+
219+
// Assess
220+
childLogger.debug('This is a log message');
221+
childLogger.info('This is an info message');
222+
223+
// Assess
224+
expect(console.debug).toBeCalledTimes(0);
225+
expect(console.info).toBeCalledTimes(0);
226+
});
227+
211228
it.each([
212229
{
213230
handlerFactory: (logger: Logger) =>

0 commit comments

Comments
 (0)