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.
Logger
s are value types with respect to the logLevel
and the metadata
(as well as the immutable label
and the selected LogHandler
). Therefore, Logger
s 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
logLevel
passed to this method is more severe than theLogger
‘slogLevel
, it will be logged, otherwise nothing will happen.Declaration
Parameters
level
The log level to log
message
at. For the available log levels, seeLogger.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.
source
The 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).
file
The 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#file
on Swift 5.2 or older).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
). -
-
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 veryLogger
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
Logger
s treatlogLevel
as a value. This means that a change inlogLevel
will 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 inlogLevel
to oneLogger
might 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
-
-
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.MetadataValue
provides convenient conformances toExpressibleByStringInterpolation
,ExpressibleByStringLiteral
,ExpressibleByArrayLiteral
, andExpressibleByDictionaryLiteral
which means that when constructingMetadataValue
s 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 MetadataValue
extension Logger.MetadataValue: Equatable
extension Logger.MetadataValue: ExpressibleByStringLiteral
extension Logger.MetadataValue: CustomStringConvertible
extension Logger.MetadataValue: ExpressibleByStringInterpolation
extension Logger.MetadataValue: ExpressibleByDictionaryLiteral
extension Logger.MetadataValue: ExpressibleByArrayLiteral
- prefer
-
The log level.
Log levels are ordered by their severity, with
See more.trace
being the least severe and.critical
being the most severe.Declaration
-
Construct a
Logger
given alabel
identifying the creator of theLogger
or a non-standardLogHandler
.The
label
should 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.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
LogHandler
s. -
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.Message
s 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