FileLogHandler

open class FileLogHandler : LogHandler

A LogHandler implementation that provides a rolling file handler implementation.

Log messages are written to a file, which is rolled if it exceeds a specified size. A certain number of backup files can be kept on disk.

If you want to write logs to file, you can add FileLogHandler as follows:

let fileLogHandler = try FileLogHandler(fileUrl: fileUrl)
Logger.root.add(handler: fileLogHandler)
  • The log level of this handler. Set to .debug by default.

    Declaration

    Swift

    public var logLevel: LogLevel
  • The formatter defines the representation of the output string for a log record.

    Declaration

    Swift

    public let formatter: LogFormatter
  • Default values for the maximum logfile size in bytes.

    Declaration

    Swift

    public static let defaultMaxFileSize: UInt64
  • Default value for the maximum number of logfiles that are stored on disk at the same time.

    Declaration

    Swift

    @available(*, deprecated, renamed: "defaultMaxFileNum")
    public static let defaultMaxBackups: Int
  • Default value for the maximum number of logfiles that are stored on disk at the same time.

    Declaration

    Swift

    public static let defaultMaxFileNum: Int
  • The actually set maxmimum size of a logfile

    Declaration

    Swift

    public var maxFileSize: UInt64
  • The actually set maximum number of files

    Declaration

    Swift

    public var maxFileNum: Int
  • Undocumented

    Declaration

    Swift

    open var isLogRedirected: Bool { get }
  • Returns the synchronization queue for synchronizing log output to files.

    Declaration

    Swift

    open class var synchronizationQueue: DispatchQueue { get }
  • Initializes the default file handler

    Throws

    LoggingError.LogFileCouldNotBeCreated(description: String) if the base file is a directory and it exists.

    Declaration

    Swift

    public init(fileURL: URL, maxFileSize: UInt64 = defaultMaxFileSize, maxFileNum: Int = defaultMaxFileNum, formatter: LogFormatter = FileLogFormatter()) throws

    Parameters

    fileUrl

    the file URL where the logfile is located. The file is created if it does not exist yet.

    maxFileSize

    the maximum size of a single log file in bytes. Cannot be negative. Passing 0 means that a file can grow unlimited in size. (Default 5MB)

    maxFileNum

    the maximum number of logfiles that are stored on disk at the same time. Cannot be less than 1. (Default 4)

    formatter

    an Formatter implementation. (Default DefaultFileFormatter)

  • Initializes the default file handler

    Throws

    LoggingError.LogFileCouldNotBeCreated(description: String) if the base file is a directory and it exists.

    Declaration

    Swift

    @available(*, deprecated, renamed: "init(fileURL:maxFileSize:maxFileNum:formatter:﹚")
    public init(fileURL: URL, maxFileSize: UInt64, maxBackups: Int, formatter: LogFormatter = FileLogFormatter()) throws

    Parameters

    fileUrl

    the file URL where the logfile is located. The file is created if it does not exist yet.

    maxFileSize

    the maximum size of a single log file in bytes. Cannot be negative. Passing 0 means that a file can grow unlimited in size. (Default 5MB)

    maxBackups

    the maximum number of logfiles that are stored on disk at the same time. Cannot be less than 1. (Default 4)

    formatter

    an Formatter implementation. (Default DefaultFileFormatter)

  • Closes this FileHandler’s file handle. This allows an application to flush any remaining log output to the file. After calling close, this FileHandler is not usable anymore and should be removed from any Logger(s) using it.

    Declaration

    Swift

    open func close()
  • Returns the list of log files that were created by FileLogHandler.

    Declaration

    Swift

    open func logFiles() -> [URL]

    Return Value

    the list of logfile URLs

  • Deletes all log files which were created after logging has been initialized.

    Throws

    FileError.OperationFailed(NSError) if one of the log files couldn’t be deleted.

    Declaration

    Swift

    open func deleteLogFiles() throws
  • Publishes a string on the file handler.

    Declaration

    Swift

    open func publish(_ record: LogRecord)

    Parameters

    message

    the message to publish.

  • Redirects the logs to a single new log file with ‘Redirected’ suffix maxFileSize is not applied to the redirected log file

    Throws

    if unable to process the redirected logs on change of value or unable to create/access the log to be written.

    Declaration

    Swift

    open func setLogRedirection(to newValue: Bool) throws

    Parameters

    newValue

    Boolean that determines if the redirected logging should be enabled(true) or disabled.

  • Returns the URL of the log file for a given backupNumber for this FileLogHandler object. Does not create the file on disk! The obtained URL may not exist on the disk.

    Declaration

    Swift

    public func getLogFileUrl(forBackupNumber backupNumber: Int = 0) -> URL?

    Parameters

    backupNumber

    Int