Skip to content

feat(logger): Emit a warning on buffer flush #3639

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion packages/logger/src/Logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1255,7 +1255,10 @@ class Logger extends Utility implements LoggerInterface {
return;
}

const buffer = this.#buffer.get(traceId) || [];
const buffer = this.#buffer.get(traceId);
if (buffer === undefined) {
return;
}

for (const item of buffer) {
const consoleMethod =
Expand All @@ -1266,6 +1269,16 @@ class Logger extends Utility implements LoggerInterface {
).toLowerCase() as keyof Omit<LogFunction, 'critical'>);
this.console[consoleMethod](item.value);
}
if (buffer.evictedLogCount > 0) {
this.printLog(
LogLevelThreshold.WARN,
this.createAndPopulateLogItem(
LogLevelThreshold.WARN,
`${buffer.evictedLogCount} were evicted from the buffer and are not shown. Increase buffer size to store more logs in the buffer`,
[]
)
);
}

this.#buffer.delete(traceId);
}
Expand Down
2 changes: 2 additions & 0 deletions packages/logger/src/logBuffer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export class SizedItem<V> {

export class SizedSet<V> extends Set<SizedItem<V>> {
public currentBytesSize = 0;
public evictedLogCount = 0;

add(item: SizedItem<V>): this {
this.currentBytesSize += item.byteSize;
Expand Down Expand Up @@ -89,6 +90,7 @@ export class CircularMap<V> extends Map<string, SizedSet<V>> {
) => {
while (buffer.currentBytesSize + item.byteSize >= this.#maxBytesSize) {
buffer.shift();
buffer.evictedLogCount++;
}
};
}
30 changes: 30 additions & 0 deletions packages/logger/tests/unit/logBuffer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,4 +107,34 @@ describe('flushBuffer', () => {
expect(console.debug).toHaveBeenCalledTimes(0);
expect(console.warn).toHaveBeenCalledTimes(1);
});

it('outputs a warning when buffered logs have been evicted', () => {
// Prepare
const logger = new TestLogger({ logLevel: 'ERROR' });
logger.enableBuffering();
logger.setbufferLevelThreshold(LogLevelThreshold.INFO);

// Act
const longMessage = 'blah'.repeat(10);

let i = 0;
while (i < 4) {
logger.info(
`${i} This is a really long log message intended to exceed the buffer ${longMessage}`
);
i++;
}

// Act
logger.flushBufferWrapper();

// Assess
expect(console.warn).toHaveLogged(
expect.objectContaining({
level: 'WARN',
message:
'1 were evicted from the buffer and are not shown. Increase buffer size to store more logs in the buffer',
})
);
});
});