4dcc4fad25
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.
27 lines
539 B
TypeScript
27 lines
539 B
TypeScript
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
|
|
}
|
|
}
|