Logger

public struct Logger

A Logger is the central type in SwiftLog. Its central function is to emit log messages using one of the methods corresponding to a log level.

The most basic usage of a Logger is

logger.info("Hello World!")
  • Log a message passing the log level as a parameter.

    If the logLevel passed to this method is more severe than the Logger‘s logLevel, it will be logged, otherwise nothing will happen.

    Declaration

    Swift

    @inlinable
    public func log(level: Logger.Level,
                    _ message: @autoclosure () -> Logger.Message,
                    metadata: @autoclosure () -> Logger.Metadata? = nil,
                    file: String = #file, function: String = #function, line: UInt = #line)

    Parameters

    level

    The log level to log message at. For the available log levels, see Logger.Level.

    message

    The message to be logged. message can be used with any string interpolation literal.

    metadata

    One-off metadata to attach to this log message

    file

    The file this log message originates from (there’s usually no need to pass it explicitly as it defaults to #file).

    function

    The function this log message originates from (there’s usually no need to pass it explicitly as it defaults to #function).

    line

    The line this log message originates from (there’s usually no need to pass it explicitly as it defaults to #line).

  • Add, change, or remove a logging metadata item.

    Note

    Logging metadata behaves as a value that means a change to the logging metadata will only affect the very Logger it was changed on.

    Declaration

    Swift

    @inlinable
    public subscript(metadataKey metadataKey: String) -> Logger.Metadata.Value? { get set }
  • Get or set the log level configured for this Logger.

    Note

    Loggers treat logLevel as a value. This means that a change in logLevel will only affect this very Logger. It it acceptable for logging backends to have some form of global log level override that affects multiple or even all loggers. This means a change in logLevel to one Logger might in certain cases have no effect.

    Declaration

    Swift

    @inlinable
    public var logLevel: Logger.Level { get set }
  • Log a message passing with the Logger.trace log level.

    If .trace is at least as severe as the Logger‘s logLevel, it will be logged, otherwise nothing will happen.

    Declaration

    Swift

    @inlinable
    public func trace(_ message: @autoclosure () -> Logger.Message,
                      metadata: @autoclosure () -> Logger.Metadata? = nil,
                      file: String = #file, function: String = #function, line: UInt = #line)

    Parameters

    level

    The log level to log message at. For the available log levels, see Logger.Level.

    message

    The message to be logged. message can be used with any string interpolation literal.

    metadata

    One-off metadata to attach to this log message

    file

    The file this log message originates from (there’s usually no need to pass it explicitly as it defaults to #file).

    function

    The function this log message originates from (there’s usually no need to pass it explicitly as it defaults to #function).

    line

    The line this log message originates from (there’s usually no need to pass it explicitly as it defaults to #line).

  • Log a message passing with the Logger.info log level.

    If .debug is at least as severe as the Logger‘s logLevel, it will be logged, otherwise nothing will happen.

    Declaration

    Swift

    @inlinable
    public func debug(_ message: @autoclosure () -> Logger.Message,
                      metadata: @autoclosure () -> Logger.Metadata? = nil,
                      file: String = #file, function: String = #function, line: UInt = #line)

    Parameters

    level

    The log level to log message at. For the available log levels, see Logger.Level.

    message

    The message to be logged. message can be used with any string interpolation literal.

    metadata

    One-off metadata to attach to this log message

    file

    The file this log message originates from (there’s usually no need to pass it explicitly as it defaults to #file).

    function

    The function this log message originates from (there’s usually no need to pass it explicitly as it defaults to #function).

    line

    The line this log message originates from (there’s usually no need to pass it explicitly as it defaults to #line).

  • Log a message passing with the Logger.Level.info log level.

    If .info is at least as severe as the Logger‘s logLevel, it will be logged, otherwise nothing will happen.

    Declaration

    Swift

    @inlinable
    public func info(_ message: @autoclosure () -> Logger.Message,
                     metadata: @autoclosure () -> Logger.Metadata? = nil,
                     file: String = #file, function: String = #function, line: UInt = #line)

    Parameters

    level

    The log level to log message at. For the available log levels, see Logger.Level.

    message

    The message to be logged. message can be used with any string interpolation literal.

    metadata

    One-off metadata to attach to this log message

    file

    The file this log message originates from (there’s usually no need to pass it explicitly as it defaults to #file).

    function

    The function this log message originates from (there’s usually no need to pass it explicitly as it defaults to #function).

    line

    The line this log message originates from (there’s usually no need to pass it explicitly as it defaults to #line).

  • Log a message passing with the Logger.Level.notice log level.

    If .notice is at least as severe as the Logger‘s logLevel, it will be logged, otherwise nothing will happen.

    Declaration

    Swift

    @inlinable
    public func notice(_ message: @autoclosure () -> Logger.Message,
                       metadata: @autoclosure () -> Logger.Metadata? = nil,
                       file: String = #file, function: String = #function, line: UInt = #line)

    Parameters

    level

    The log level to log message at. For the available log levels, see Logger.Level.

    message

    The message to be logged. message can be used with any string interpolation literal.

    metadata

    One-off metadata to attach to this log message

    file

    The file this log message originates from (there’s usually no need to pass it explicitly as it defaults to #file).

    function

    The function this log message originates from (there’s usually no need to pass it explicitly as it defaults to #function).

    line

    The line this log message originates from (there’s usually no need to pass it explicitly as it defaults to #line).

  • Log a message passing with the Logger.Level.warning log level.

    If .warning is at least as severe as the Logger‘s logLevel, it will be logged, otherwise nothing will happen.

    Declaration

    Swift

    @inlinable
    public func warning(_ message: @autoclosure () -> Logger.Message,
                        metadata: @autoclosure () -> Logger.Metadata? = nil,
                        file: String = #file, function: String = #function, line: UInt = #line)

    Parameters

    level

    The log level to log message at. For the available log levels, see Logger.Level.

    message

    The message to be logged. message can be used with any string interpolation literal.

    metadata

    One-off metadata to attach to this log message

    file

    The file this log message originates from (there’s usually no need to pass it explicitly as it defaults to #file).

    function

    The function this log message originates from (there’s usually no need to pass it explicitly as it defaults to #function).

    line

    The line this log message originates from (there’s usually no need to pass it explicitly as it defaults to #line).

  • Log a message passing with the Logger.Level.error log level.

    If .error is at least as severe as the Logger‘s logLevel, it will be logged, otherwise nothing will happen.

    Declaration

    Swift

    @inlinable
    public func error(_ message: @autoclosure () -> Logger.Message,
                      metadata: @autoclosure () -> Logger.Metadata? = nil,
                      file: String = #file, function: String = #function, line: UInt = #line)

    Parameters

    level

    The log level to log message at. For the available log levels, see Logger.Level.

    message

    The message to be logged. message can be used with any string interpolation literal.

    metadata

    One-off metadata to attach to this log message

    file

    The file this log message originates from (there’s usually no need to pass it explicitly as it defaults to #file).

    function

    The function this log message originates from (there’s usually no need to pass it explicitly as it defaults to #function).

    line

    The line this log message originates from (there’s usually no need to pass it explicitly as it defaults to #line).

  • Log a message passing with the Logger.Level.critical log level.

    .critical messages will always be logged.

    Declaration

    Swift

    @inlinable
    public func critical(_ message: @autoclosure () -> Logger.Message,
                         metadata: @autoclosure () -> Logger.Metadata? = nil,
                         file: String = #file, function: String = #function, line: UInt = #line)

    Parameters

    level

    The log level to log message at. For the available log levels, see Logger.Level.

    message

    The message to be logged. message can be used with any string interpolation literal.

    metadata

    One-off metadata to attach to this log message

    file

    The file this log message originates from (there’s usually no need to pass it explicitly as it defaults to #file).

    function

    The function this log message originates from (there’s usually no need to pass it explicitly as it defaults to #function).

    line

    The line this log message originates from (there’s usually no need to pass it explicitly as it defaults to #line).

  • Metadata is a typealias for [String: Logger.MetadataValue] the type of the metadata storage.

    Declaration

    Swift

    public typealias Metadata = [String : MetadataValue]
  • A logging metadata value. Logger.MetadataValue is string, array, and dictionary literal convertible.

    See more

    Declaration

    Swift

    public enum MetadataValue
  • The log level.

    Raw values of log levels correspond to their severity, and are ordered by lowest numeric value (0) being the most severe. The raw values match the syslog values.

    See more

    Declaration

    Swift

    public enum Level : CaseIterable
  • Construct a Logger given a label identifying the creator of the Logger.

    The label should identify the creator of the Logger. This can be an application, a sub-system, or even a datatype.

    Declaration

    Swift

    public init(label: String)

    Parameters

    label

    An identifier for the creator of a Logger.

  • Construct a Logger given a label identifying the creator of the Logger or a non-standard LogHandler.

    The label should identify the creator of the Logger. This can be an application, a sub-system, or even a datatype.

    This initializer provides an escape hatch in case the global default logging backend implementation (set up using LoggingSystem.bootstrap is not appropriate for this particular logger.

    Declaration

    Swift

    public init(label: String, factory: (String) -> LogHandler)

    Parameters

    label

    An identifier for the creator of a Logger.

    factory

    A closure creating non-standard LogHandlers.

  • Logger.Message represents a log message’s text. It is usually created using string literals.

    Example creating a Logger.Message:

    let world: String = "world"
    let myLogMessage: Logger.Message = "Hello \(world)"
    

    Most commonly, Logger.Messages appear simply as the parameter to a logging method such as:

    logger.info("Hello \(world)")
    
    See more

    Declaration

    Swift

    public struct Message: ExpressibleByStringLiteral,
        Equatable,
        CustomStringConvertible,
        ExpressibleByStringInterpolation