Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions packages/logger/src/Logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -408,9 +408,6 @@ class Logger extends Utility implements LoggerInterface {
* @param extraInput - The extra input to log.
*/
public error(input: LogItemMessage, ...extraInput: LogItemExtraInput): void {
if (this.#bufferConfig.enabled && this.#bufferConfig.flushOnErrorLog) {
this.flushBuffer();
}
this.processLogItem(LogLevelThreshold.ERROR, input, extraInput);
}

Expand Down Expand Up @@ -1063,6 +1060,15 @@ class Logger extends Utility implements LoggerInterface {
input: LogItemMessage,
extraInput: LogItemExtraInput
): void {
if (
this.#bufferConfig.enabled &&
this.#bufferConfig.flushOnErrorLog &&
logLevel >= LogLevelThreshold.ERROR &&
logLevel < LogLevelThreshold.SILENT
) {
this.flushBuffer();
}

const traceId = getXRayTraceIdFromEnv();
if (traceId !== undefined && this.shouldBufferLog(traceId, logLevel)) {
try {
Expand Down
32 changes: 32 additions & 0 deletions packages/logger/tests/unit/logBuffer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,38 @@ describe('Buffer logs', () => {
expect(console.error).toBeCalledTimes(1);
});

it('it flushes the buffer when a critical log is logged', () => {
// Prepare
const logger = new Logger({
logLevel: LogLevel.ERROR,
logBufferOptions: { enabled: true },
});

// Act
logger.debug('This is a log message');
logger.critical('This is a critical message');

// Assess
expect(console.debug).toHaveBeenCalledTimes(1);
expect(console.error).toHaveBeenCalledTimes(1); // critical uses console.error
});

it('does not flush on critical logs when flushOnErrorLog is disabled', () => {
// Prepare
const logger = new Logger({
logLevel: LogLevel.ERROR,
logBufferOptions: { enabled: true, flushOnErrorLog: false },
});

// Act
logger.debug('This is a log message');
logger.critical('This is a critical message');

// Assess
expect(console.debug).toHaveBeenCalledTimes(0);
expect(console.error).toHaveBeenCalledTimes(1); // critical uses console.error
});

it('passes down the same buffer config to child loggers', () => {
// Prepare
const logger = new Logger({
Expand Down
Loading