Add BufferLogAppender #1
Reference in New Issue
Block a user
Delete Branch "buffer-log-appender"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
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
clearBufferon theBufferLogAppender instance to avoid the buffer array growing without
limit.
PR Review: Add BufferLogAppender
Summary
This PR adds a
BufferLogAppenderthat collects log messages into a shared buffer array, with a configurable log-level threshold regime. The concept is solid, but the implementation has several issues that need attention.Issues
Critical
Missing
LogAppenderimport —implements LogAppenderon line 10 referencesLogAppenderwhich is never imported. TypeScript will fail to compile. Likely needs:buffer.pushinstead ofthis.buffer.push— Line 22:buffer.push(msg)references an undefined variablebuffer. This will throw aReferenceErrorat runtime. Should be:Important
Falsy threshold check rejects valid level 0 — Line 21:
if (this.threshold && msg.level < this.threshold)uses truthiness onthreshold. IfLogLevel.ALLis0(or any valid level enum value that equals zero), thethis.thresholdcheck will short-circuit and never filter messages, even when the user explicitly set a threshold. This also meansthresholdset to0in the constructor would be treated as unset on the next call, silently falling back toLogLevel.ALL.Consider:
Or restructure so the default
LogLevel.ALLis only used whenthresholdisundefined:Then the runtime check becomes simply
if (msg.level < this.threshold) return.Suggestion
Consider making
bufferdefault-initialized — The constructor requires callers to pass in a buffer. Not necessarily wrong, but consider an overload or default that creates a fresh empty array when none is provided:Missing tests — No test file is included in this PR. A new public class with filtering logic should have at minimum: basic message buffering, threshold honoring, and
clearBuffer()behavior.Verdict
Request changes — the missing import and the
buffer.pushbug are compile/runtime blockers. Please fix these and I'll re-review.@mahseiah_ai Please re-review. I believe your comments have been addressed.
Re-review — All Issues Resolved ✅
The updates in commit
f9cb676("Properly set default thresholds on log appenders") address all three issues from my previous review:LogAppenderimport — Addedimport type { LogAppender } from './log-appender'on line 1.LogLevelas type-only import — Changed toimport { LogLevel, type LogMessage }—LogLevelis now a regular import supporting runtime value access, whileLogMessageremains type-only.buffer → this.buffer— Fixed tothis.buffer.push(msg).Additionally, the
index.tsnow correctly re-exportsbuffer-log-appender(already present in the first commit).Bonus Improvement
The second commit also cleans up threshold initialization across
ConsoleLogAppenderandApiLogAppender, using the??null-coalescing pattern consistently:this.threshold = threshold ?? LogLevel.ALL. Nice consistent pattern.Buffer constructor change
The
bufferparameter is now optional (buffer?: LogMessage[]), defaulting to an empty array — a good DX improvement.No further issues. LGTM! 🚀