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.
Loggers are value types with respect to the logLevel and the metadata (as well as the immutable label
and the selected LogHandler). Therefore, Loggers are suitable to be passed around between libraries if you want
to preserve metadata across libraries.
The most basic usage of a Logger is
logger.info("Hello World!")
-
An identifier of the creator of this
Logger.Declaration
Swift
public let label: String -
Log a message passing the log level as a parameter.
If the
logLevelpassed to this method is more severe than theLogger‘slogLevel, it will be logged, otherwise nothing will happen.Declaration
Parameters
levelThe log level to log
messageat. For the available log levels, seeLogger.Level.messageThe message to be logged.
messagecan be used with any string interpolation literal.metadataOne-off metadata to attach to this log message.
sourceThe source this log messages originates from. Defaults to the module emitting the log message (on Swift 5.3 or newer and the folder name containing the log emitting file on Swift 5.2 or older).
fileThe file this log message originates from (there’s usually no need to pass it explicitly as it defaults to
#fileID(on Swift 5.3 or newer and#fileon Swift 5.2 or older).functionThe function this log message originates from (there’s usually no need to pass it explicitly as it defaults to
#function).lineThe line this log message originates from (there’s usually no need to pass it explicitly as it defaults to
#line). -
-
Undocumented
Declaration
-
-
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 veryLoggerit 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 treatlogLevelas a value. This means that a change inlogLevelwill only affect this veryLogger. It is acceptable for logging backends to have some form of global log level override that affects multiple or even all loggers. This means a change inlogLevelto oneLoggermight in certain cases have no effect.Declaration
Swift
@inlinable public var logLevel: Logger.Level { get set } -
Undocumented
Declaration
-
-
Undocumented
Declaration
-
-
Undocumented
Declaration
-
-
Undocumented
Declaration
-
-
Undocumented
Declaration
-
-
Undocumented
Declaration
-
-
Undocumented
Declaration
-
-
Undocumented
Declaration
-
-
Undocumented
Declaration
-
-
Undocumented
Declaration
-
-
Undocumented
Declaration
-
-
Undocumented
Declaration
-
-
Undocumented
Declaration
-
-
Undocumented
Declaration
-
-
Metadatais a typealias for[String: Logger.MetadataValue]the type of the metadata storage.Declaration
Swift
public typealias Metadata = [String : MetadataValue] -
A logging metadata value.
Logger.MetadataValueis string, array, and dictionary literal convertible.MetadataValueprovides convenient conformances toExpressibleByStringInterpolation,ExpressibleByStringLiteral,ExpressibleByArrayLiteral, andExpressibleByDictionaryLiteralwhich means that when constructingMetadataValues you should default to using Swift’s usual literals.Examples:
- prefer
logger.info("user logged in", metadata: ["user-id": "\(user.id)"])over..., metadata: ["user-id": .string(user.id.description)]) - prefer
logger.info("user selected colors", metadata: ["colors": ["\(user.topColor)", "\(user.secondColor)"]])over..., metadata: ["colors": .array([.string("\(user.topColor)"), .string("\(user.secondColor)")]) - prefer
logger.info("nested info", metadata: ["nested": ["fave-numbers": ["\(1)", "\(2)", "\(3)"], "foo": "bar"]])over..., metadata: ["nested": .dictionary(["fave-numbers": ...])])
Declaration
Swift
public enum MetadataValueextension Logger.MetadataValue: Equatableextension Logger.MetadataValue: ExpressibleByStringLiteralextension Logger.MetadataValue: CustomStringConvertibleextension Logger.MetadataValue: ExpressibleByStringInterpolationextension Logger.MetadataValue: ExpressibleByDictionaryLiteralextension Logger.MetadataValue: ExpressibleByArrayLiteral - prefer
-
The log level.
Log levels are ordered by their severity, with
See more.tracebeing the least severe and.criticalbeing the most severe.Declaration
-
Construct a
Loggergiven alabelidentifying the creator of theLoggeror a non-standardLogHandler.The
labelshould identify the creator of theLogger. 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.bootstrapis not appropriate for this particular logger.Declaration
Swift
public init(label: String, factory: (String) -> LogHandler)Parameters
labelAn identifier for the creator of a
Logger.factoryA closure creating non-standard
LogHandlers. -
Logger.Messagerepresents 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:
See morelogger.info("Hello \(world)")Declaration
Swift
public struct Message : ExpressibleByStringLiteral, Equatable, CustomStringConvertible, ExpressibleByStringInterpolation
View on GitHub
Install in Dash
Logger Structure Reference