FieldBasedLogFormatter
open class FieldBasedLogFormatter: ConcatenatingLogFormatter
The FieldBasedLogFormatter
provides a simple interface for constructing
a customized LogFormatter
by specifying different fields.
Let’s say you wanted to construct a LogFormatter
that outputs the following
fields separated by tabs:
- The
LogEntry
‘stimestamp
property as a UNIX time value - The
severity
of theLogEntry
as a numeric value - The
Payload
of theLogEntry
You could do this by constructing a FieldBasedLogFormatter
as follows:
let formatter = FieldBasedLogFormatter(fields: [.timestamp(.unix),
.delimiter(.tab),
.severity(.numeric),
.delimiter(.tab),
.payload])
-
The individual
See moreField
declarations for theFieldBasedLogFormatter
.Declaration
Swift
public enum Field
-
Initializes the
FieldBasedLogFormatter
to use the specified fields.Declaration
Swift
public init(fields: [Field], hardFail: Bool = false)
Parameters
fields
The
Field
s that will be used by the receiver.hardFail
Determines the behavior of
format(_:)
when one of the receiver’sformatters
returnsnil
. Whenfalse
, if any formatter returnsnil
, it is simply excluded from the concatenation, but formatting continues. Unless none of theformatters
returns a string, the receiver will always return a non-nil
value. However, whenhardFail
istrue
, all of theformatters
must return strings; if any formatter returnsnil
, the receiver also returnsnil
. -
Initializes the
FieldBasedLogFormatter
to use the specified formatters.Declaration
Swift
public override init(formatters: [LogFormatter], hardFail: Bool = false)
Parameters
formatters
The
LogFormatter
s that will be used by the receiver.hardFail
Determines the behavior of
format(_:)
when one of the receiver’sformatters
returnsnil
. Whenfalse
, if any formatter returnsnil
, it is simply excluded from the concatenation, but formatting continues. Unless none of theformatters
returns a string, the receiver will always return a non-nil
value. However, whenhardFail
istrue
, all of theformatters
must return strings; if any formatter returnsnil
, the receiver also returnsnil
.