RotatingLogFileRecorder

open class RotatingLogFileRecorder: LogRecorderBase

A LogRecorder implementation that maintains a set of daily rotating log files, kept for a user-specified number of days.

Important

A RotatingLogFileRecorder instance assumes full control over the log directory specified by its directoryPath property. Please see the initializer documentation for details.
  • The number of days for which the receiver will retain log files before they’re eligible for pruning.

    Declaration

    Swift

    open let daysToKeep: Int
  • The filesystem path to a directory where the log files will be stored.

    Declaration

    Swift

    open let directoryPath: String
  • Initializes a new RotatingLogFileRecorder instance.

    Warning

    The RotatingLogFileRecorder expects to have full control over the contents of its directoryPath. Any file not recognized as an active log file will be deleted during the automatic pruning process, which may occur at any time. Therefore, be extremely careful when constructing the value passed in as the directoryPath.

    Declaration

    Swift

    public init(daysToKeep: Int, directoryPath: String, formatters: [LogFormatter] = [ReadableLogFormatter()])

    Parameters

    daysToKeep

    The number of days for which log files should be retained.

    directoryPath

    The filesystem path of the directory where the log files will be stored. Please note the warning above regarding the directoryPath.

    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.

  • Returns a string representing the filename that will be used to store logs recorded on the given date.

    Declaration

    Swift

    open class func logFilename(forDate date: Date)
            -> String

    Parameters

    date

    The Date for which the log file name is desired.

    Return Value

    The filename.

  • Attempts to create—if it does not already exist—the directory indicated by the directoryPath property.

    Throws

    If the function fails to create a directory at directoryPath.

    Declaration

    Swift

    open func createLogDirectory()
            throws
  • Called by the LogReceptacle to record the specified 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.

  • Deletes any expired log files (and any other detritus that may be hanging around inside our directoryPath).

    Warning

    Any file within the directoryPath not recognized as an active log file will be deleted during pruning.

    Declaration

    Swift

    open func prune()