Event

public struct Event : Encodable

An Event structure holds the information for a complete loggable event in your application.

You are not expected to construct an Event yourself. Instead, use an EventBuilder to set up your events. When you tell the event builder to send the event, it creates an Event for you and sends it to the event sink.

The only way to access the data in an Event is by using an encoder to encode it.

Event also includes some static properties that serve a few purposes:

  • Provide some special EventBuilders: current and global.
  • Allow configuring the behavior of events.
  • Encodes the data in the event to an Encoder.

    For encoders that care about the order of keys that are encoded, events encode fields in the following order:

    • Event.Key.error, if present
    • Event.Key.message
    • Any app-specific custom keys, sorted alphabetically.

    Declaration

    Swift

    public func encode(to encoder: Encoder) throws

    Parameters

    encoder

    The encoder to use to encode the event data.

  • An event builder for fields that will be added to all future events.

    The global event builder is not meant to be sent. Instead, any fields you include here will be automatically included when sending future events.

    Declaration

    Swift

    public static var global: EventBuilder { get set }
  • The event that is currently in progress.

    In most cases, this is the event that your app should be adding fields to. This allows uncoupled parts of your app to add relevant information to the current event without knowing who started it or when it will be sent.

    The current event is the same throughout the entire app, so if you are performing background or concurrent tasks, you may want to avoid the current event for those and explicitly create and pass around an EventBuilder.

    EventBuilder is a value type, so all changes to the current event should be made directly on this property, rather than trying to store the current event somewhere else.

    // good
    Event.current[.myField] = "foo"
    
    // bad
    var event = Event.current
    event[.myField] = "foo"
    

    Declaration

    Swift

    public static var current: EventBuilder { get set }
  • Key

    A key identifier for the different fields you can add to events.

    There are a few predefined keys for fields that will be handled automatically by the Events system, but most fields will be custom, defined by the needs of your application. You can add custom keys in extensions of Event.Key in your app:

    extension Event.Key {
        static let florbCount: Event.Key = "florb_count"
    }
    

    Then you can use this key to set fields on events in your app:

    Event.current[.florbCount] = florbs.count
    

    The string literal you assign for the key will be the key used for the field when encoding the event.

    Declaration

    Swift

    public struct Key : Hashable, CodingKey, ExpressibleByStringLiteral
  • The default event sink for events in your application.

    New EventBuilders will use this sink to send their events if no sink is passed in explicitly. In general, you probably don’t want to worry about keeping track of event sinks throughout your app, so when your app launches, you should set this to the sink you want to use.

    The default sink sends events to the default os_log logger. It is recommended when using os_log to define a custom subsystem and category specifically for logging your events. To do this, set this property to a custom instance of OSLogEventSink:

    Event.sink = OSLogEventSink(subsystem: "com.myapp.App", category: "events")
    

    Declaration

    Swift

    public static var sink: EventSink