RecordingLogSink class base

A sink for tests: records what it receives and counts the lifecycle calls.

LogSink is a base class so that its lifecycle (add throws after close, onClose runs once, attach reaches wrappers' inner sinks) is inherited and cannot be bypassed by a reimplementation; that also rules out implements LogSink in a mock. This sink is the test double instead: attach it to a root and assert on entries, or hand it to a wrapper under test as inner and assert on what the wrapper let through, flushed and closed.

final sink = RecordingLogSink();
final logger = RootLogger(sinks: [sink], dispatch: LogDispatchPolicy.synchronous)..finishStartup();
service(logger.child(tags: {'sync'})).run();
expect(sink.messages, ['Sync started', 'Sync finished']);

failOn makes add throw for the entries it selects, to test how a logger or wrapper handles a failing sink. batches groups the entries by dispatch batch (LogSink.endBatch). Subclass it (extends) for a test that needs more, for example a hook at flush time.

Inheritance

Constructors

RecordingLogSink({bool failOn(LogEntry entry)?})

Properties

batches → List<List<LogEntry>>
Entries grouped by dispatch batch: a new list per endBatch that ended a non-empty batch. Entries since the last batch end are in entries but not yet here.
final
closeCount ↔ int
getter/setter pair
diagnostics → DiagnosticReporter
Where the sink reports its own failures. Set by the logger on attach (attach); before that, DiagnosticReporter.console.
no setterinherited
entries → List<LogEntry>
Entries received, in order; an entry failOn rejected is not among them.
final
failOn ↔ bool Function(LogEntry entry)?
Entries for which add throws a StateError; null means none.
getter/setter pair
flushCount ↔ int
getter/setter pair
hashCode → int
The hash code for this object.
no setterinherited
isClosed → bool
Whether close has been called: from then on add throws.
no setterinherited
messages → Iterable<String>
no setter
name → String
Sink name in library messages: in diagnostics (LogDiagnostic) and in the dropped-entries warnings that go into the log. Defaults to the class name, but in an --obfuscate build that is unreadable (minified:aB); the library's sinks return a constant string, and a custom sink should do the same.
no setteroverride
runtimeType → Type
A representation of the runtime type of the object.
no setterinherited

Methods

add(LogEntry entry) → void
Accepts an entry (onAdd). Throws StateError after close: logging into a closed sink is a shutdown-order bug, better seen than silently sent to a closed store. The RootLogger fan-out catches such errors and reports them to diagnostics.
inherited
attach(DiagnosticReporter diagnostics) → void
Tells the sink where to send reports of its own failures (diagnostics). Called by the logger on attach (RootLogger.addSink and the sinks from the root constructor); wrappers pass the channel to their inner sinks (onAttach). A repeated call replaces the channel: a sink reattached to another logger reports to it.
inherited
close() → Future<void>
Shuts the sink down: no new entries are accepted, then onClose delivers the accepted ones and releases resources. A repeated call closes nothing again and returns the result of the first.
inherited
endBatch() → void
Signals that a batch of entries has ended (onBatchEnd): RootLogger calls it after every queue dispatch, including dispatch inside the logger call (a fatal entry, flush, close). A sink that writes in batches (LogStoreSink) hands over what it accumulated here: everything the root dispatched at once goes as one batch, and a fatal entry goes together with what accumulated before it, before log returns. Does nothing after close.
inherited
flush() → Future<void>
Waits until accepted entries reach their destination (for example are persisted to the store) without closing the sink (onFlush). After close, waits for the closing to complete, which delivers everything accepted.
inherited
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
onAdd(LogEntry entry) → void
Handles an accepted entry. Called only before close.
override
onAttach(DiagnosticReporter diagnostics) → void
Called from attach: pass diagnostics to inner sinks. Does nothing by default; DelegatingSink passes it to inner.
inherited
onBatchEnd() → void
Called from endBatch. Does nothing by default: for sinks that handle each entry in onAdd; wrappers (DelegatingSink) pass the end of batch to inner.
override
onClose() → Future<void>
Delivers accepted entries and releases resources (for example closes the store). Called once. Does nothing by default.
override
onFlush() → Future<void>
Waits for accepted entries to be delivered. Does nothing by default: for sinks that deliver entries immediately in onAdd.
override
toString() → String
A string representation of this object.
inherited

Operators

operator ==(Object other) → bool
The equality operator.
inherited