BufferedLogRecorder
open class BufferedLogRecorder<BufferItem>: LogRecorderBase
The BufferedLogRecorder
is a generic LogRecorder
that buffers the log
messages passed to its record()
function.
Construction requires a createBufferItem
function, which is responsible
for converting the LogEntry
and formatted message String
into the
generic BufferItem
type.
Three specific subclasses are also provided which will buffer a different
BufferItem
type for each call to record()
:
BufferedMessageRecorder
storesString
s containing the formatted log message.BufferedLogEntryRecorder
stores theLogEntry
values passed torecord()
.BufferedLogEntryMessageRecorder
stores a(LogEntry, String)
tuple containing theLogEntry
and formatted log message.
-
The maximum number if items that will be stored in the receiver’s buffer
Declaration
Swift
open let bufferLimit: Int
-
The buffer, an array of
BufferItem
s created to represent theLogEntry
values recorded by the receiver.Declaration
Swift
open private(set) var buffer: [BufferItem]
-
Initializes a new
BufferedLogRecorder
.Declaration
Swift
public init(formatters: [LogFormatter], bufferLimit: Int = 10_000, queue: DispatchQueue? = nil, createBufferItem: @escaping (LogEntry, String) -> BufferItem)
Parameters
formatters
An array of
LogFormatter
s to use for formatting log entries to be recorded by the receiver. Each formatter is consulted in sequence, and the formatted string returned by the first formatter to yield a non-nil
value will be recorded. If every formatter returnsnil
, the log entry is silently ignored and not recorded.bufferLimit
If this value is positive, it specifies the maximum number of items to store in the buffer. If
record()
is called when the buffer limit has been reached, the oldest item in the buffer will be dropped. If this value is zero or negative, no limit will be applied. Note that this is potentially dangerous in production code, since memory consumption will grow endlessly unless you manually clear the buffer periodically.queue
The
DispatchQueue
to use for the recorder. Ifnil
, a new queue will be created.createBufferItem
The function used to create
BufferItem
instances for eachLogEntry
and formatted message string passed to the receiver’srecord
()` function. -
Called by the
LogReceptacle
to record the formatted log message.Note
This function is only called if one of the
formatters
associated with the receiver returned a non-nil
string for the givenLogEntry
.Declaration
Swift
open override func record(message: String, for entry: LogEntry, currentQueue: DispatchQueue, synchronousMode: Bool)
Parameters
message
The message to record.
entry
The
LogEntry
for whichmessage
was created.currentQueue
The GCD queue on which the function is being executed.
synchronousMode
If
true
, the recording is being done in synchronous mode, and the recorder should act accordingly. -
Clears the contents of the buffer.
This operation is performed synchronously on the receiver’s
queue
to ensure thread safety.Declaration
Swift
public func clear()