FileWriter
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.
-
Default constructor for this writer.
See also
FileWriter.Default
for default values to for this class.See also
FileWriter.Strategy
for strategies available to configure this writer.See also
OutputStreamFormatter
for more information about formatters that can be used.Throws
An
Error
type should the init fail.Declaration
Swift
public init(directory: URL, strategy: Strategy = Default.strategy, format: OutputStreamFormatter = Default.format) throws
Parameters
directory
A URL that points to the directory where the FileWriter should write the log files.
strategy
The
FileWriter.Strategy
to use for managing file on the storage device.format
An instance of an
OutputStreamFormatter
used to format the output before writing to the file.
-
Required write function for the logger
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
-
Defaults for init values
See moreDeclaration
Swift
public enum Default
-
A
See moreStrategy
defines howFileWriter
will manage file naming and physical file management.Declaration
Swift
public enum Strategy