JSONFormat
public struct JSONFormat : OutputStreamFormatter
The JSONFormat is a configurable implementation of a OutputStreamFormatter
which outputs standard JSON as it’s output.
Since the JSONFormat is an instance of OutputStreamFormatter
it can be
used with any Writer
that accepts the OutputStreamFormatter
on construction.
JSONFormat has a number of options for configuring it for many use-cases. All options have a default value assigned to them to make it easy to get started without configuration. Should refinement of the default behavior be required, these options give you fine grain control over the output.
Specifying Attributes
All attributes that are output as JSON are configurable. The default output is a set of all attributes TraceLog outputs, these are:
- timestamp
- level
- tag
- message
- processName
- processIdentifier
- threadIdentifier
- file
- function
- line
Special formatting options
JSONFormat has special processing options available for output.
The options available are:
prettyPrint: Add formatting characters to print the output in a more human friendly form.
/* With `.prettyPrint` */ { "timestamp" : 28800.0, "level" : "INFO", "tag" : "TestTag", "message" : "Test message.", "processName" : "TestProcess", "processIdentifier" : 120, "threadIdentifier" : 200, "file" : "JSONFormatTests.swift", "function" : "testAttributeDefaultList()", "line" : 240 } /* Without `.prettyPrint` */ {"timestamp":28800.0,"level":"INFO","tag":"TestTag","message":"Test message.",,"processName":"TestProcess","processIdentifier":120,"threadIdentifier":200,"file":"JSONFormatTests.swift","function":"testAttributeDefaultList()","line":240}
Line terminator
Each log entry formatted by the formatter can be terminated with a character sequence.
The default value is a newline (,\n
) and can be changed by passing the terminator
parameter at construction.
For instance.
let formatter = JSONFormat(terminator: "\r\n")
In this case we changed the terminator from the default of ,\n
to \r\n
. The characters
can be any characters that make sense for your application.
-
The designated initializer for this type.
Declaration
Parameters
attributes
A set of
JSONFormat.Attribute
s the will be formatted for output.options
A Set of
JSONFormat.Option
s to allow optional formatting control.terminator
A string that will be output at the end of the output to terminate the entry.
-
The encoding that will be used to encode the output output of
bytes(from:)
.Value:
String.Encoding.utf8
Note
JSON text exchanged between systems that are not part of a closed ecosystem MUST be encoded using UTF-8 [RFC3629].Declaration
Swift
public let encoding: String.Encoding
-
Text conversion function required by the
OutputStreamFormatter
protocol.See also
OutputStreamFormatter
for more information about this function.Declaration
Swift
public func bytes(from entry: Writer.LogEntry) -> Result<[UInt8], OutputStreamFormatterError>
-
Default values used for
See moreJSONFormat
Declaration
Swift
public enum Default
-
Declaration
Swift
public enum Attribute : Int, CaseIterable
-
Declaration
Swift
public enum Option : Hashable