LogBuffer class

The most recent log records, kept in memory so the application can show them: records, filtering with LogQuery, live streams, counters and text export.

A mobile or desktop app has no terminal its users can read. With this buffer an app can offer a log screen, a "send logs" button for bug reports, or an error badge in a debug menu. The logging system keeps one, LoggingSystem.buffer, which receives every record; it is also the logBuffer bean, so a screen can get it injected. Its size and threshold come from logging.buffer.capacity and logging.threshold.buffer, and logging.buffer.enabled=false turns it off. There is no Spring counterpart.

// Flutter: a live list, filtered by what the user typed in `search`.
final query = LogQuery(minLevel: SpringLogLevel.info, text: search);
StreamBuilder<List<SpringLogRecord>>(
  stream: buffer.watch(query),
  builder: (context, snapshot) => ListView(
    children: [
      for (final record in snapshot.data ?? const <SpringLogRecord>[])
        ListTile(
          key: ValueKey(record.sequence),
          title: Text(record.message),
          subtitle: Text('${record.loggerName} · ${record.time}'),
        ),
    ],
  ),
);

buffer.counts[SpringLogLevel.error]; // the number on an error badge
buffer.export(query);                // text to share or copy
buffer.clear();                      // the "clear" button
Inheritance

Constructors

LogBuffer({int capacity = 1000, SpringLogLevel threshold = SpringLogLevel.trace})
A buffer keeping the last capacity records (at least 1, else an ArgumentError) of level threshold and above. Applications use the logging system's buffer rather than create one.

Properties

capacity ↔ int
How many records are kept: when a new record would exceed it, the oldest is dropped and counted in dropped. Lowering it drops the oldest records at once. It must be at least 1, else an ArgumentError is thrown.
getter/setter pair
counts → Map<SpringLogLevel, int>
How many kept records each level has, for badges. Every level but SpringLogLevel.off is a key, with 0 when it has no record.
no setter
dropped → int
How many records were dropped because the buffer was full, since the buffer was created or last cleared; useful for a "1 200 older lines not shown" note.
no setter
hashCode → int
The hash code for this object.
no setterinherited
isEmpty → bool
Whether no record is kept.
no setter
length → int
How many records are kept now; at most capacity.
no setter
loggerNames → List<String>
The names of the loggers that have a kept record, sorted and without repeats, to fill a filter menu.
no setter
onChange → Stream<void>
An event after each burst of changes (records added or dropped, the buffer cleared or made smaller): many records logged in one synchronous run give one event, at the end of the microtask.
no setter
onRecord → Stream<SpringLogRecord>
Each record as it enters the buffer, delivered synchronously during the logging call. Use it to react to single records, such as showing a snack bar on an error; to redraw a list, watch is cheaper.
no setter
records → List<SpringLogRecord>
The kept records, oldest first, as an unmodifiable copy that does not change when new records arrive.
no setter
runtimeType → Type
A representation of the runtime type of the object.
no setterinherited
threshold ↔ SpringLogLevel
The lowest level this appender writes, checked after the logger levels; SpringLogLevel.off writes nothing.
getter/setter pairinherited

Methods

append(SpringLogRecord record) → void
Called by the logging system for each record: passes it to write when it is at or above threshold, and drops it otherwise.
inherited
clear() → void
Removes every record and resets dropped to 0, for a "clear" button; watch streams then emit an empty list.
close() → void
Releases what the appender holds, such as an open file. Called by LoggingSystem.removeAppender; does nothing by default.
inherited
export([LogQuery query = const LogQuery(), LogPattern? pattern]) → String
The records matching query as text, for sharing or copying to the clipboard: each record formatted with pattern, joined by line breaks.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
query([LogQuery query = const LogQuery()]) → List<SpringLogRecord>
The kept records that match query (all of them by default), oldest first, as an unmodifiable list.
toString() → String
A string representation of this object.
override
watch([LogQuery query = const LogQuery()]) → Stream<List<SpringLogRecord>>
The records matching query as a stream of lists, made for Flutter's StreamBuilder: the current list when listened to, then a new list after each burst of changes that alters the matching records. A burst that adds only records the query rejects emits nothing.
write(SpringLogRecord record) → void
Writes record to this output. Implement it in a subclass; the record has already passed threshold. It runs synchronously on the logging call, so slow work (a network call) should be queued.
override

Operators

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