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.
This commit is contained in:
2026-05-05 06:25:09 -05:00
parent 895a8c42ca
commit 4dcc4fad25
+26
View File
@@ -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
}
}