mirror of
https://github.com/apple/swift-log
synced 2026-11-24 03:47:49 +00:00
Replace the flat-parameter `log(level:message:metadata:source:file:function:line:)` method on `LogHandler` with `log(event: LogEvent)`, enabling forward-compatible evolution of the `LogHandler` interface without breaking existing handler implementations. ### Motivation: Changes to the `LogHandler` API require dance around creating new overloads, deprecating old overloads, which unnecessary bloats `LogHandler` protocol surface (like happened with the source: parameter). We have upcoming changes that would introduce new capabilities to the `LogHandler` protocol, for example https://forums.swift.org/t/proposal-slg-0003-standardized-error-metadata-via-logger-convenience/84518/35. It makes sense to introduce a single container structure for all the data associated with a log event before introducing any new overloads in `LogHandler` protocol. ### Modifications: - New struct `LogEvent` is added. - `LogHandler` protocol now has an additional `log(event: LogEvent)` method. - `Logger` is calling handler's `log(event: LogEvent)` method. ### Result: Further changes to the `LogEvent` with new information passed from the `Logger` to a `LogHandler` does not require changes in the `LogHandler` API. As a bonus, `LogEvent` makes `source` a computable variable, improving performance for log handlers who do not need the default source created based on the log file location.
88 lines
3.2 KiB
Swift
88 lines
3.2 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
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
/// A log event that contains all the information about a single log statement.
|
|
///
|
|
/// `LogEvent` is passed to a ``LogHandler`` via ``LogHandler/log(event:)`` and carries every piece of data emitted
|
|
/// at the log call site. Handlers may inspect, modify, or forward any of the event's properties.
|
|
///
|
|
/// All properties are mutable so that handler wrappers (for example ``MultiplexLogHandler``) can rewrite fields
|
|
/// such as metadata or source before forwarding to downstream handlers.
|
|
public struct LogEvent: Sendable {
|
|
/// The log level of this event.
|
|
public var level: Logger.Level
|
|
|
|
/// The message of this event.
|
|
public var message: Logger.Message
|
|
|
|
/// The metadata associated with this event, if any.
|
|
public var metadata: Logger.Metadata? {
|
|
get { self._metadata }
|
|
set { self._metadata = newValue }
|
|
}
|
|
|
|
private var _metadata: Logger.Metadata?
|
|
|
|
/// The source where this log event originated, for example the logging module.
|
|
///
|
|
/// When no explicit source was provided at the call site, this is derived lazily from ``file``
|
|
/// every time this property is accessed. Handlers that never read this property pay no
|
|
/// allocation cost for source computation.
|
|
public var source: String {
|
|
@inlinable get { self._source ?? Logger.currentModule(fileID: self.file) }
|
|
@inlinable set { self._source = newValue }
|
|
}
|
|
|
|
@usableFromInline
|
|
internal var _source: String?
|
|
|
|
/// The file this log event originates from.
|
|
public var file: String
|
|
|
|
/// The function this log event originates from.
|
|
public var function: String
|
|
|
|
/// The line this log event originates from.
|
|
public var line: UInt
|
|
|
|
/// Creates a new log event.
|
|
///
|
|
/// - Parameters:
|
|
/// - level: The log level of this event.
|
|
/// - message: The message of this event.
|
|
/// - metadata: The metadata associated with this event, if any.
|
|
/// - source: The source where this log event originated. When `nil`, ``source`` is derived lazily
|
|
/// from ``file`` on first access so handlers that never read it pay no allocation cost.
|
|
/// - file: The file this log event originates from.
|
|
/// - function: The function this log event originates from.
|
|
/// - line: The line this log event originates from.
|
|
public init(
|
|
level: Logger.Level,
|
|
message: Logger.Message,
|
|
metadata: Logger.Metadata?,
|
|
source: String?,
|
|
file: String,
|
|
function: String,
|
|
line: UInt
|
|
) {
|
|
self.level = level
|
|
self.message = message
|
|
self._metadata = metadata
|
|
self._source = source
|
|
self.file = file
|
|
self.function = function
|
|
self.line = line
|
|
}
|
|
}
|