Writers

Writers are used to write the log output to various endpoints.

Writers are installed using the TraceLog.configure(writers:environment:) and TraceLog.configure(mode:writers:environment:) functions.

Note: Writers are not meant to be used directly other than creating one. You create a writer and pass it to TraceLog through the configure function. Once that is done, TraceLog will call the writer for you.

  • ConsoleWriter is the default Writer in TraceLog and writes to stdout.

    This writer will be installed for you if you do not set any other writer’s at configuration time. Its a basic writer that can be used at any time that you want the output to go to the stdout.

    Creating a ConsoleWriter is simple using the built in defaults.

        let writer = ConsoleWriter()
    
        TraceLog.configure(writers: [writer])
    

    Output Format

    Since ConsoleWriter 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 writer = ConsoleWriter(format: JSONFormat())
    
        TraceLog.configure(writers: [writer])
    
    See more

    Declaration

    Swift

    public class ConsoleWriter : 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.
    See more

    Declaration

    Swift

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