mirror of
https://github.com/apple/swift-log
synced 2026-11-25 03:57:48 +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>
157 lines
4.5 KiB
Swift
157 lines
4.5 KiB
Swift
//===----------------------------------------------------------------------===//
|
|
//
|
|
// This source file is part of the Swift Logging API open source project
|
|
//
|
|
// Copyright (c) 2018-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 InMemoryLogging
|
|
import Logging
|
|
import Testing
|
|
|
|
struct InMemoryLogHandlerTests {
|
|
@Test
|
|
func collectsLogs() {
|
|
let (logHandler, logger) = self.makeTestLogger()
|
|
logger.info("hello", metadata: ["key1": "value1", "key2": ["a", "b", "c"]])
|
|
|
|
#expect(
|
|
logHandler.entries == [
|
|
InMemoryLogHandler.Entry(
|
|
level: .info,
|
|
message: "hello",
|
|
metadata: ["key1": "value1", "key2": ["a", "b", "c"]]
|
|
)
|
|
]
|
|
)
|
|
}
|
|
|
|
@Test
|
|
func metadataFromLoggerEndsUpInEntry() {
|
|
var (logHandler, logger) = self.makeTestLogger()
|
|
logger[metadataKey: "test"] = "value"
|
|
logger.info("hello", metadata: ["key1": "value1", "key2": ["a", "b", "c"]])
|
|
|
|
#expect(
|
|
logHandler.entries == [
|
|
InMemoryLogHandler.Entry(
|
|
level: .info,
|
|
message: "hello",
|
|
metadata: ["key1": "value1", "key2": ["a", "b", "c"], "test": "value"]
|
|
)
|
|
]
|
|
)
|
|
// Metadata also sticks onto the logger
|
|
#expect(logger[metadataKey: "test"] == "value")
|
|
}
|
|
|
|
@Test
|
|
func moreSpecificMetadataOverridesGlobal() {
|
|
let testProvider = Logger.MetadataProvider {
|
|
["a": "1", "b": "1", "c": "1"]
|
|
}
|
|
var (logHandler, logger) = self.makeTestLogger(metadataProvider: testProvider)
|
|
logger[metadataKey: "b"] = "2"
|
|
logger[metadataKey: "c"] = "2"
|
|
logger.info("hello", metadata: ["c": "3"])
|
|
|
|
#expect(
|
|
logHandler.entries == [
|
|
InMemoryLogHandler.Entry(level: .info, message: "hello", metadata: ["a": "1", "b": "2", "c": "3"])
|
|
]
|
|
)
|
|
}
|
|
|
|
@Test
|
|
func errorEquality() throws {
|
|
let (logHandler, logger) = self.makeTestLogger()
|
|
logger.info("hello", error: TestError.first)
|
|
let entry = try #require(logHandler.entries.first)
|
|
|
|
#expect(
|
|
entry
|
|
== InMemoryLogHandler.Entry(
|
|
level: .info,
|
|
message: "hello",
|
|
error: TestError.first,
|
|
metadata: [:],
|
|
)
|
|
)
|
|
|
|
#expect(
|
|
entry
|
|
!= InMemoryLogHandler.Entry(
|
|
level: .info,
|
|
message: "hello",
|
|
error: nil,
|
|
metadata: [:],
|
|
)
|
|
)
|
|
|
|
#expect(
|
|
entry
|
|
!= InMemoryLogHandler.Entry(
|
|
level: .info,
|
|
message: "hello",
|
|
error: TestError.second,
|
|
metadata: [:]
|
|
)
|
|
)
|
|
|
|
#expect(
|
|
entry
|
|
!= InMemoryLogHandler.Entry(
|
|
level: .info,
|
|
message: "hello",
|
|
error: Nested.TestError.first,
|
|
metadata: [:]
|
|
)
|
|
)
|
|
}
|
|
|
|
@Test
|
|
func clear() {
|
|
let (logHandler, logger) = self.makeTestLogger()
|
|
logger.info("hello", metadata: ["key1": "value1", "key2": ["a", "b", "c"]])
|
|
logHandler.clear()
|
|
logger.info("hello2")
|
|
|
|
// Only hello2 is here
|
|
#expect(
|
|
logHandler.entries == [
|
|
InMemoryLogHandler.Entry(level: .info, message: "hello2", metadata: [:])
|
|
]
|
|
)
|
|
}
|
|
|
|
private func makeTestLogger(metadataProvider: Logger.MetadataProvider? = nil) -> (InMemoryLogHandler, Logger) {
|
|
var logHandler = InMemoryLogHandler()
|
|
logHandler.metadataProvider = metadataProvider
|
|
let logger = Logger(
|
|
label: "MyApp",
|
|
factory: { _ in
|
|
logHandler
|
|
}
|
|
)
|
|
return (logHandler, logger)
|
|
}
|
|
|
|
enum TestError: Error {
|
|
case first
|
|
case second
|
|
}
|
|
|
|
struct Nested {
|
|
enum TestError: Error {
|
|
case first
|
|
}
|
|
}
|
|
}
|