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():

  • The maximum number if items that will be stored in the receiver’s buffer

    Declaration

    Swift

    open let bufferLimit: Int
  • The function used to create a BufferItem given a LogEntry and a formatted message string.

    Declaration

    Swift

    open let createBufferItem: (LogEntry, String) -> BufferItem
  • The buffer, an array of BufferItems created to represent the LogEntry 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 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-nil value will be recorded. If every formatter returns nil, 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. If nil, a new queue will be created.

    createBufferItem

    The function used to create BufferItem instances for each LogEntry and formatted message string passed to the receiver’s record()` 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 given LogEntry.

    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 which message 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()