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():
BufferedMessageRecorderstoresStrings containing the formatted log message.BufferedLogEntryRecorderstores theLogEntryvalues passed torecord().BufferedLogEntryMessageRecorderstores a(LogEntry, String)tuple containing theLogEntryand 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
BufferItems created to represent theLogEntryvalues 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
formattersAn array of
LogFormatters 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-nilvalue will be recorded. If every formatter returnsnil, the log entry is silently ignored and not recorded.bufferLimitIf 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.queueThe
DispatchQueueto use for the recorder. Ifnil, a new queue will be created.createBufferItemThe function used to create
BufferIteminstances for eachLogEntryand formatted message string passed to the receiver’srecord()` function. -
Called by the
LogReceptacleto record the formatted log message.Note
This function is only called if one of the
formattersassociated with the receiver returned a non-nilstring for the givenLogEntry.Declaration
Swift
open override func record(message: String, for entry: LogEntry, currentQueue: DispatchQueue, synchronousMode: Bool)Parameters
messageThe message to record.
entryThe
LogEntryfor whichmessagewas created.currentQueueThe GCD queue on which the function is being executed.
synchronousModeIf
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
queueto ensure thread safety.Declaration
Swift
public func clear()
View on GitHub
BufferedLogRecorder Class Reference