FileWriter

@available(iOSApplicationExtension, unavailable, message: "FileWriter can not be initialized in an Extension.  Please initialize it in the main App.")
public class FileWriter : OutputStreamWriter

The FileWriter is a fully configurable TraceLog OutputStreamWriter implementation that writes it’s formatted data to files on persistent storage.

FileWriter allows file handling strategies to to be configured that determine how the files are named and handled on the storage device.

Creating a FileWriter is simple using the built in defaults.

    let fileWriter = try FileWriter(directory: URL(fileURLWithPath: "./"))

    TraceLog.configure(writers: [fileWriter])

File Strategies

FileWriter allows various file management strategies to be configured and used for management of the files on the storage device. The default strategy is the .fixed strategy which will create a fixed file that is continually appended to and never rotated. If you require a file rotation strategy the .rotate(at:) strategy will allow you to set various rotation criteria for the files created.

To rotate the file every time the the FIleWriter is started, you can pass the .startup option to the rotate strategy as in this example.

    let fileWriter = try FileWriter(directory: URL(fileURLWithPath: "./"), strategy: .rotate(at: [.startup]))

    TraceLog.configure(writers: [fileWriter])

The .rotate strategy allows for changing the naming template of the file written to the storage device. If you change the default naming you must consider the frequency that your files may rotate so that naming conflicts do not occur. The default template is suitable for almost all strategies that may be used since it names files based on date with millisecond precision.

Output Format

Since FileWriter is an instance of OutputStreamWriter it allows you to specify the format of the output with any instance of OutputStreamFormatter. The default format is TextFormat with the default TextFormat options. You can easily change the format by overriding the default on creation.

    let fileWriter = try FileWriter(directory: URL(fileURLWithPath: "./"), format: JSONFormat())

    TraceLog.configure(writers: [fileWriter])

See also

FileWriter.Strategy for complete details of all strategies that can be used.

Initialization

OutputStreamWriter conformance

  • Required write function for the logger

    See also

    Writer for more information about the write function.

    See also

    ‘Writer.LogEntry’ for a complete definition of the loggable entry.

    See also

    FailureReason for failure return types.

    Declaration

    Swift

    public func write(_ entry: Writer.LogEntry) -> Result<Int, FailureReason>
  • OutputStreamFormatter being used for formating output.

    See also

    OutputStreamWriter for more information about the protocol and the format field.

    Declaration

    Swift

    public let format: OutputStreamFormatter

Default Values

  • Defaults for init values

    See more

    Declaration

    Swift

    public enum Default

Available Strategies

  • A Strategy defines how FileWriter will manage file naming and physical file management.

    See more

    Declaration

    Swift

    public enum Strategy