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‘s timestamp property as a UNIX time value
  • The severity of the LogEntry as a numeric value
  • The Payload of the LogEntry

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 Field declarations for the FieldBasedLogFormatter.

    See more

    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 Fields that will be used by the receiver.

    hardFail

    Determines the behavior of format(_:) when one of the receiver’s formatters returns nil. When false, if any formatter returns nil, it is simply excluded from the concatenation, but formatting continues. Unless none of the formatters returns a string, the receiver will always return a non-nil value. However, when hardFail is true, all of the formatters must return strings; if any formatter returns nil, the receiver also returns nil.

  • Initializes the FieldBasedLogFormatter to use the specified formatters.

    Declaration

    Swift

    public override init(formatters: [LogFormatter], hardFail: Bool = false)

    Parameters

    formatters

    The LogFormatters that will be used by the receiver.

    hardFail

    Determines the behavior of format(_:) when one of the receiver’s formatters returns nil. When false, if any formatter returns nil, it is simply excluded from the concatenation, but formatting continues. Unless none of the formatters returns a string, the receiver will always return a non-nil value. However, when hardFail is true, all of the formatters must return strings; if any formatter returns nil, the receiver also returns nil.