mirror of
https://github.com/apple/swift-log
synced 2026-11-22 03:40:39 +00:00
Add new methods on `Logger` that allows the caller to attach `Error`s to
log events, which will be propagated to the `LogHandler` as-is.
### Motivation:
Error information is typically of high interest in logs and log
analysis. Currently, however, there is no standardized way of attaching
`Error` information to logs, forcing users to either append it to the
log message (`logger.warning("Something went wrong: \(error)")`), or
attach it as metadata. By providing a separate parameter for this, and
passing it as-is to the `LogHandler`, we both provide a standard,
visible way to attach it, while also letting the `LogHandler` format it
as it sees fit.
Closes #291
### Modifications:
* New methods on `Logger` that accept an (optional) `Error`.
* An `error` property is added to `LogEvent`, which contains the `Error`
from the new `Logger` methods.
* Adjust `LogHandler` implementations to serialize error as metadata.
### Result:
Users of `Logger` can use the new methods to attach `Error`s to their
logs, which should help unify how `Error`s get logged in various code
bases.
`LogHandler` implementations can use the new `error` property on
`LogEvent` and serialize it in a way that's suitable for that
implementation. In general, extracting the string representation and the
(qualified) type name should be a good baseline.
---------
Co-authored-by: Vladimir Kukushkin <kukushechkin@mac.com>
52 lines
1.8 KiB
Swift
52 lines
1.8 KiB
Swift
//===----------------------------------------------------------------------===//
|
|
//
|
|
// This source file is part of the Swift Logging API open source project
|
|
//
|
|
// Copyright (c) 2022 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
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
import Testing
|
|
|
|
@testable import Logging
|
|
|
|
struct SendableTest {
|
|
@Test func sendableLogger() async {
|
|
let testLogging = TestLogging()
|
|
|
|
let logger = Logger(
|
|
label: "test",
|
|
factory: {
|
|
testLogging.make(label: $0)
|
|
}
|
|
)
|
|
let message1 = Logger.Message(stringLiteral: "critical 1")
|
|
let message2 = Logger.Message(stringLiteral: "critical 2")
|
|
let metadata: Logger.Metadata = ["key": "value"]
|
|
let error = TestError()
|
|
|
|
let task = Task.detached {
|
|
logger.info("info")
|
|
logger.critical(message1)
|
|
logger.critical(message2)
|
|
logger.warning(.init(stringLiteral: "warning"), metadata: metadata)
|
|
logger.warning("error", error: error)
|
|
}
|
|
|
|
await task.value
|
|
testLogging.history.assertExist(level: .info, message: "info", metadata: [:])
|
|
testLogging.history.assertExist(level: .critical, message: "critical 1", metadata: [:])
|
|
testLogging.history.assertExist(level: .critical, message: "critical 2", metadata: [:])
|
|
testLogging.history.assertExist(level: .warning, message: "warning", metadata: metadata)
|
|
testLogging.history.assertExist(level: .warning, message: "error", error: error)
|
|
}
|
|
|
|
final class TestError: Error {}
|
|
}
|