Files
swift-log/Sources/Logging/Logging.swift
T

640 lines
29 KiB
Swift

//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift Logging API open source project
//
// Copyright (c) 2018-2019 Apple Inc. and the Swift Logging API project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of Swift Logging API project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
#if os(macOS) || os(iOS) || os(tvOS) || os(watchOS)
import Darwin
#else
import Glibc
#endif
/// A `LogHandler` is an implementation of a logging backend.
///
/// This type is an implementation detail and should not normally be used, unless implementing your own logging backend.
/// For user API of please refer to the documentation of `Logger`.
public protocol LogHandler {
/// This functions is called when a `LogHandler` must emit a log message. There is no need for the `LogHandler` to
/// check if the `level` is above or below the configured `logLevel` as `Logger` already performed this check and
/// determined that a message should be logged.
///
/// - parameters:
/// - level: The log level the message was logged at.
/// - message: The message to log. To objtain a `String` represenation call `message.description`.
/// - metadata: The metadata associated to this log message.
/// - file: The file the log message was emitted from.
/// - function: The function the log line was emitted from.
/// -line: The line the log message was emitted from.
func log(level: Logger.Level,
message: Logger.Message,
metadata: Logger.Metadata?,
file: String, function: String, line: UInt)
/// Add, remove, or change the logging metadata.
///
/// - note: `LogHandler`s must treat logging metadata as a value type. This means that the change in metadata must
/// only affect this very `LogHandler`.
///
/// - parameters:
/// - metadataKey: The key for the metadata item
subscript(metadataKey _: String) -> Logger.Metadata.Value? { get set }
/// Get or set the entire metadata storage as a dictionary.
///
/// - note: `LogHandler`s must treat logging metadata as a value type. This means that the change in metadata must
/// only affect this very `LogHandler`.
var metadata: Logger.Metadata { get set }
/// Get or set the configured log level.
///
/// - note: `LogHandler`s must treat the log level as a value type. This means that the change in metadata must
/// only affect this very `LogHandler`. It is acceptable to provide some form of global log level override
/// that means a change in log level on a particular `LogHandler` might not be reflected in any
/// `LogHandler`.
var logLevel: Logger.Level { get set }
}
/// A `Logger` is the central type in `swift-log`. 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!")
///
public struct Logger {
@usableFromInline
var handler: LogHandler
public let label: String
internal init(label: String, _ handler: LogHandler) {
self.label = label
self.handler = handler
}
}
extension Logger {
/// 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.
///
/// - 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 `#file`.
/// - line: The line this log message originates from (there's usually no need to pass it explicitly as it
/// defaults to `#line`.
@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) {
if self.logLevel >= level {
self.handler.log(level: level, message: message(), metadata: metadata(), file: file, function: function, line: 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.
@inlinable
public subscript(metadataKey metadataKey: String) -> Logger.Metadata.Value? {
get {
return self.handler[metadataKey: metadataKey]
}
set {
self.handler[metadataKey: metadataKey] = newValue
}
}
/// Get or set the entire metadata storage.
///
/// - 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.
@inlinable
public var metadata: Logger.Metadata {
get {
return self.handler.metadata
}
set {
self.handler.metadata = newValue
}
}
/// Get or set the log level configured for this `Logger`.
///
/// - note: `Logger`s 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.
@inlinable
public var logLevel: Logger.Level {
get {
return self.handler.logLevel
}
set {
self.handler.logLevel = newValue
}
}
}
extension Logger {
/// Log a message passing with the `Logger.info` log level.
///
/// If `.debug` is more severe than the `Logger`'s `logLevel`, it will be logged, otherwise nothing will happen.
///
/// - 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 `#file`.
/// - line: The line this log message originates from (there's usually no need to pass it explicitly as it
/// defaults to `#line`.
@inlinable
public func debug(_ message: @autoclosure () -> Logger.Message, metadata: @autoclosure () -> Logger.Metadata? = nil, file: String = #file, function: String = #function, line: UInt = #line) {
self.log(level: .debug, message(), metadata: metadata(), file: file, function: function, line: line)
}
/// Log a message passing with the `Logger.Level.info` log level.
///
/// If `.info` is more severe than the `Logger`'s `logLevel`, it will be logged, otherwise nothing will happen.
///
/// - 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 `#file`.
/// - line: The line this log message originates from (there's usually no need to pass it explicitly as it
/// defaults to `#line`.
@inlinable
public func info(_ message: @autoclosure () -> Logger.Message, metadata: @autoclosure () -> Logger.Metadata? = nil, file: String = #file, function: String = #function, line: UInt = #line) {
self.log(level: .info, message(), metadata: metadata(), file: file, function: function, line: line)
}
/// Log a message passing with the `Logger.Level.notice` log level.
///
/// If `.notice` is more severe than the `Logger`'s `logLevel`, it will be logged, otherwise nothing will happen.
///
/// - 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 `#file`.
/// - line: The line this log message originates from (there's usually no need to pass it explicitly as it
/// defaults to `#line`.
@inlinable
public func notice(_ message: @autoclosure () -> Logger.Message, metadata: @autoclosure () -> Logger.Metadata? = nil, file: String = #file, function: String = #function, line: UInt = #line) {
self.log(level: .notice, message(), metadata: metadata(), file: file, function: function, line: line)
}
/// Log a message passing with the `Logger.Level.warning` log level.
///
/// If `.warning` is more severe than the `Logger`'s `logLevel`, it will be logged, otherwise nothing will happen.
///
/// - 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 `#file`.
/// - line: The line this log message originates from (there's usually no need to pass it explicitly as it
/// defaults to `#line`.
@inlinable
public func warning(_ message: @autoclosure () -> Logger.Message, metadata: @autoclosure () -> Logger.Metadata? = nil, file: String = #file, function: String = #function, line: UInt = #line) {
self.log(level: .warning, message(), metadata: metadata(), file: file, function: function, line: line)
}
/// Log a message passing with the `Logger.Level.error` log level.
///
/// If `.error` is more severe than the `Logger`'s `logLevel`, it will be logged, otherwise nothing will happen.
///
/// - 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 `#file`.
/// - line: The line this log message originates from (there's usually no need to pass it explicitly as it
/// defaults to `#line`.
@inlinable
public func error(_ message: @autoclosure () -> Logger.Message, metadata: @autoclosure () -> Logger.Metadata? = nil, file: String = #file, function: String = #function, line: UInt = #line) {
self.log(level: .error, message(), metadata: metadata(), file: file, function: function, line: line)
}
/// Log a message passing with the `Logger.Level.critical` log level.
///
/// If `.critical` is more severe than the `Logger`'s `logLevel`, it will be logged, otherwise nothing will happen.
///
/// - 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 `#file`.
/// - line: The line this log message originates from (there's usually no need to pass it explicitly as it
/// defaults to `#line`.
@inlinable
public func critical(_ message: @autoclosure () -> Logger.Message, metadata: @autoclosure () -> Logger.Metadata? = nil, file: String = #file, function: String = #function, line: UInt = #line) {
self.log(level: .critical, message(), metadata: metadata(), file: file, function: function, line: line)
}
/// Log a message passing with the `Logger.Level.alert` log level.
///
/// If `.alert` is more severe than the `Logger`'s `logLevel`, it will be logged, otherwise nothing will happen.
///
/// - 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 `#file`.
/// - line: The line this log message originates from (there's usually no need to pass it explicitly as it
/// defaults to `#line`.
@inlinable
public func alert(_ message: @autoclosure () -> Logger.Message, metadata: @autoclosure () -> Logger.Metadata? = nil, file: String = #file, function: String = #function, line: UInt = #line) {
self.log(level: .alert, message(), metadata: metadata(), file: file, function: function, line: line)
}
/// Log a message passing with the `Logger.Level.emergency` log level.
///
/// If `.emergency` is more severe than the `Logger`'s `logLevel`, it will be logged, otherwise nothing will happen.
///
/// - 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 `#file`.
/// - line: The line this log message originates from (there's usually no need to pass it explicitly as it
/// defaults to `#line`.
@inlinable
public func emergency(_ message: @autoclosure () -> Logger.Message, metadata: @autoclosure () -> Logger.Metadata? = nil, file: String = #file, function: String = #function, line: UInt = #line) {
self.log(level: .emergency, message(), metadata: metadata(), file: file, function: function, line: line)
}
}
/// The `LoggingSystem` is a global facility where the default logging backend implementation (`LogHandler`) can be
/// configured. `LoggingSystem` is set up just once in a given program to set up the desired logging backend
/// implementation.
public enum LoggingSystem {
fileprivate static let lock = ReadWriteLock()
fileprivate static var factory: (String) -> LogHandler = StdoutLogHandler.init
fileprivate static var initialized = false
/// `bootstrap` is a one-time configuration function which globally selects the desired logging backend
/// implementation. `bootstrap` can be called at maximum once in any given program, calling it more than once will
/// lead to undefined behaviour, most likely a crash.
///
/// - parameters:
/// - factory: A closure that given a `Logger` identifier, produces an instance of the `LogHandler`.
public static func bootstrap(_ factory: @escaping (String) -> LogHandler) {
lock.withWriterLock {
precondition(!self.initialized, "logging system can only be initialized once per process.")
self.factory = factory
self.initialized = true
}
}
// for our testing we want to allow multiple bootstraping
internal static func bootstrapInternal(_ factory: @escaping (String) -> LogHandler) {
self.lock.withWriterLock {
self.factory = factory
}
}
}
extension Logger {
/// `Metadata` is a typealias for `[String: Logger.MetadataValue]` the type of the metadata storage.
public typealias Metadata = [String: MetadataValue]
/// A logging metadata value. `Logger.MetadataValue` is string, array, and dictionary literal convertible.
public enum MetadataValue {
/// A metadata value which is a `String`.
case string(String)
/// A metadata value which is some `CustomStringConvertible`.
case stringConvertible(CustomStringConvertible)
/// A metadata value which is a dictionary from `String` to `Logger.MetadataValue`.
case dictionary(Metadata)
/// A metadata value which is an array of `Logger.MetadataValue`s.
case array([Metadata.Value])
}
/// 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.
public enum Level: Int {
/// Appropriate for messages that contain information normally of use only when
/// debugging a program.
case debug = 7
/// Appropriate for informational messages.
case info = 6
/// Appropriate for conditions that are not error conditions, but that may require
/// special handling.
case notice = 5
/// Appropriate for messages that are not error conditions, but more severe than
/// `.notice`.
case warning = 4
/// Appropriate for error conditions.
case error = 3
/// Appropriate for criticial error conditions that usually require immediate
/// attention.
case critical = 2
/// Appropriate for conditions that should be corrected immediately, such as a corrupted
/// system database.
case alert = 1
/// Appropriate for panic conditions.
case emergency = 0
}
/// 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.
///
/// - parameters:
/// - label: An identifier for the creator of a `Logger`.
public init(label: String) {
self = LoggingSystem.lock.withReaderLock { Logger(label: label, LoggingSystem.factory(label)) }
}
/// 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.
///
/// - parameters:
/// - label: An identifier for the creator of a `Logger`.
/// - factory: A closure creating non-standard `LogHandler`s.
public init(label: String, factory: (String) -> LogHandler) {
self = Logger(label: label, factory(label))
}
}
extension Logger.Level: Comparable {
public static func < (lhs: Logger.Level, rhs: Logger.Level) -> Bool {
return lhs.rawValue < rhs.rawValue
}
}
// Extension has to be done on explicit type rather than Logger.Metadata.Value as workaround for: https://bugs.swift.org/browse/SR-9687
// Then we could write it as follows and it would work under Swift 5 and not only 4 as it does currently:
// extension Logger.Metadata.Value: Equatable {
extension Logger.MetadataValue: Equatable {
public static func == (lhs: Logger.Metadata.Value, rhs: Logger.Metadata.Value) -> Bool {
switch (lhs, rhs) {
case (.string(let lhs), .string(let rhs)):
return lhs == rhs
case (.stringConvertible(let lhs), .stringConvertible(let rhs)):
return lhs.description == rhs.description
case (.array(let lhs), .array(let rhs)):
return lhs == rhs
case (.dictionary(let lhs), .dictionary(let rhs)):
return lhs == rhs
default:
return false
}
}
}
extension Logger {
/// `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:
///
/// logger.info("Hello \(world)")
///
public struct Message: ExpressibleByStringLiteral, Equatable, CustomStringConvertible, ExpressibleByStringInterpolation {
public typealias StringLiteralType = String
private var value: String
public init(stringLiteral value: String) {
self.value = value
}
public var description: String {
return self.value
}
}
}
/// A pseudo-`LogHandler` that can be used to send messages to multiple other `LogHandler`s.
///
/// The first `LogHandler` passed to the initialisation function of `MultiplexLogHandler` control the `logLevel` as
/// well as the `metadata` for this `LogHandler`. Any subsequent `LogHandler`s used to initialise a
/// `MultiplexLogHandler` are merely to emit the log message to another place.
public struct MultiplexLogHandler: LogHandler {
private var handlers: [LogHandler]
public init(_ handlers: [LogHandler]) {
assert(handlers.count > 0)
self.handlers = handlers
}
public var logLevel: Logger.Level {
get {
return self.handlers[0].logLevel
}
set {
self.mutatingForEachHandler {
$0.logLevel = newValue
}
}
}
public func log(level: Logger.Level, message: Logger.Message, metadata: Logger.Metadata?, file: String, function: String, line: UInt) {
self.handlers.forEach { handler in
handler.log(level: level, message: message, metadata: metadata, file: file, function: function, line: line)
}
}
public var metadata: Logger.Metadata {
get {
return self.handlers[0].metadata
}
set {
self.mutatingForEachHandler { $0.metadata = newValue }
}
}
public subscript(metadataKey metadataKey: String) -> Logger.Metadata.Value? {
get {
return self.handlers[0].metadata[metadataKey]
}
set {
self.mutatingForEachHandler { $0[metadataKey: metadataKey] = newValue }
}
}
private mutating func mutatingForEachHandler(_ mutator: (inout LogHandler) -> Void) {
for index in self.handlers.indices {
mutator(&self.handlers[index])
}
}
}
/// Ships with the logging module, really boring just prints something using the `print` function
internal struct StdoutLogHandler: LogHandler {
private let lock = Lock()
public init(label: String) {}
private var _logLevel: Logger.Level = .info
public var logLevel: Logger.Level {
get {
return self.lock.withLock { self._logLevel }
}
set {
self.lock.withLock {
self._logLevel = newValue
}
}
}
private var prettyMetadata: String?
private var _metadata = Logger.Metadata() {
didSet {
self.prettyMetadata = self.prettify(self._metadata)
}
}
public func log(level: Logger.Level, message: Logger.Message, metadata: Logger.Metadata?, file: String, function: String, line: UInt) {
let prettyMetadata = metadata?.isEmpty ?? true ? self.prettyMetadata : self.prettify(self.metadata.merging(metadata!, uniquingKeysWith: { _, new in new }))
print("\(self.timestamp()) \(level):\(prettyMetadata.map { " \($0)" } ?? "") \(message)")
}
public var metadata: Logger.Metadata {
get {
return self.lock.withLock { self._metadata }
}
set {
self.lock.withLock { self._metadata = newValue }
}
}
public subscript(metadataKey metadataKey: String) -> Logger.Metadata.Value? {
get {
return self.lock.withLock { self._metadata[metadataKey] }
}
set {
self.lock.withLock {
self._metadata[metadataKey] = newValue
}
}
}
private func prettify(_ metadata: Logger.Metadata) -> String? {
return !metadata.isEmpty ? metadata.map { "\($0)=\($1)" }.joined(separator: " ") : nil
}
private func timestamp() -> String {
var buffer = [Int8](repeating: 0, count: 255)
var timestamp = time(nil)
let localTime = localtime(&timestamp)
strftime(&buffer, buffer.count, "%Y-%m-%dT%H:%M:%S%z", localTime)
return buffer.withUnsafeBufferPointer {
$0.withMemoryRebound(to: CChar.self) {
String(cString: $0.baseAddress!)
}
}
}
}
// Extension has to be done on explicit type rather than Logger.Metadata.Value as workaround for: https://bugs.swift.org/browse/SR-9687
extension Logger.MetadataValue: ExpressibleByStringLiteral {
public typealias StringLiteralType = String
public init(stringLiteral value: String) {
self = .string(value)
}
}
extension Logger.MetadataValue: CustomStringConvertible {
public var description: String {
switch self {
case .dictionary(let dict):
return dict.mapValues { $0.description }.description
case .array(let list):
return list.map { $0.description }.description
case .string(let str):
return str
case .stringConvertible(let repr):
return repr.description
}
}
}
// Extension has to be done on explicit type rather than Logger.Metadata.Value as workaround for: https://bugs.swift.org/browse/SR-9687
extension Logger.MetadataValue: ExpressibleByStringInterpolation {
}
// Extension has to be done on explicit type rather than Logger.Metadata.Value as workaround for: https://bugs.swift.org/browse/SR-9687
extension Logger.MetadataValue: ExpressibleByDictionaryLiteral {
public typealias Key = String
public typealias Value = Logger.Metadata.Value
public init(dictionaryLiteral elements: (String, Logger.Metadata.Value)...) {
self = .dictionary(.init(uniqueKeysWithValues: elements))
}
}
// Extension has to be done on explicit type rather than Logger.Metadata.Value as workaround for: https://bugs.swift.org/browse/SR-9687
extension Logger.MetadataValue: ExpressibleByArrayLiteral {
public typealias ArrayLiteralElement = Logger.Metadata.Value
public init(arrayLiteral elements: Logger.Metadata.Value...) {
self = .array(elements)
}
}