From 4dcc4fad25d1c320108df65526cbbd0b1eafe095 Mon Sep 17 00:00:00 2001 From: Jonathan Bernard Date: Tue, 5 May 2026 06:25:09 -0500 Subject: [PATCH] Add BufferLogAppender Sometimes it is useful to capture log messages in a way that is easy for the running program to introspect or manually handle. *BufferLogAppender* captures the logs in a simple array. Be careful, there is no default flush/clear mechanism that runs automatically. Users of BufferLogAppender should take care to render/handle log messages and periodically call `clearBuffer` on the *BufferLogAppender* instance to avoid the buffer array growing without limit. --- src/buffer-log-appender.ts | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 src/buffer-log-appender.ts diff --git a/src/buffer-log-appender.ts b/src/buffer-log-appender.ts new file mode 100644 index 0000000..edf8dea --- /dev/null +++ b/src/buffer-log-appender.ts @@ -0,0 +1,26 @@ +import type { + LogLevel, + LogMessage +} from './log-message' + +export class BufferLogAppender implements LogAppender { + public threshold = LogLevel.ALL + public buffer: LogMessage[] + + constructor(buffer: LogMessage[], threshold?: LogLevel) { + this.buffer = buffer + + if (threshold) { + this.threshold = threshold + } + } + + public appendMessage(msg: LogMessage): void { + if (this.threshold && msg.level < this.threshold) return + else buffer.push(msg) + } + + public clearBuffer(): void { + this.buffer.length = 0 + } +}