From 3e3ef75109d6801b2c44504e73f55f0dce6662c9 Mon Sep 17 00:00:00 2001 From: Moritz Lang Date: Wed, 18 Jan 2023 21:41:36 +0100 Subject: [PATCH] Emit correct metadata from StreamLogHandler (#251) Co-authored-by: Konrad `ktoso` Malawski --- Sources/Logging/Logging.swift | 2 +- Tests/LoggingTests/LoggingTest+XCTest.swift | 1 + Tests/LoggingTests/LoggingTest.swift | 18 ++++++++++++++++++ 3 files changed, 20 insertions(+), 1 deletion(-) diff --git a/Sources/Logging/Logging.swift b/Sources/Logging/Logging.swift index 983ccf2c..af8d2b11 100644 --- a/Sources/Logging/Logging.swift +++ b/Sources/Logging/Logging.swift @@ -1297,7 +1297,7 @@ public struct StreamLogHandler: LogHandler { metadata.merge(explicit, uniquingKeysWith: { _, explicit in explicit }) } - return explicit + return metadata } private func prettify(_ metadata: Logger.Metadata) -> String? { diff --git a/Tests/LoggingTests/LoggingTest+XCTest.swift b/Tests/LoggingTests/LoggingTest+XCTest.swift index 99ce0bba..db55b092 100644 --- a/Tests/LoggingTests/LoggingTest+XCTest.swift +++ b/Tests/LoggingTests/LoggingTest+XCTest.swift @@ -56,6 +56,7 @@ extension LoggingTest { ("testStreamLogHandlerOutputFormat", testStreamLogHandlerOutputFormat), ("testStreamLogHandlerOutputFormatWithMetaData", testStreamLogHandlerOutputFormatWithMetaData), ("testStreamLogHandlerOutputFormatWithOrderedMetadata", testStreamLogHandlerOutputFormatWithOrderedMetadata), + ("testStreamLogHandlerWritesIncludeMetadataProviderMetadata", testStreamLogHandlerWritesIncludeMetadataProviderMetadata), ("testStdioOutputStreamWrite", testStdioOutputStreamWrite), ("testStdioOutputStreamFlush", testStdioOutputStreamFlush), ("testOverloadingError", testOverloadingError), diff --git a/Tests/LoggingTests/LoggingTest.swift b/Tests/LoggingTests/LoggingTest.swift index da47c22a..48604c68 100644 --- a/Tests/LoggingTests/LoggingTest.swift +++ b/Tests/LoggingTests/LoggingTest.swift @@ -824,6 +824,24 @@ class LoggingTest: XCTestCase { XCTAssert(interceptStream.strings[1].contains("a=a1 b=b1"), "LINES: \(interceptStream.strings[1])") } + func testStreamLogHandlerWritesIncludeMetadataProviderMetadata() { + let interceptStream = InterceptStream() + LoggingSystem.bootstrapInternal({ _, metadataProvider in + StreamLogHandler(label: "test", stream: interceptStream, metadataProvider: metadataProvider) + }, metadataProvider: .exampleMetadataProvider) + let log = Logger(label: "test") + + let testString = "my message is better than yours" + log.critical("\(testString)") + + let messageSucceeded = interceptStream.interceptedText?.trimmingCharacters(in: .whitespacesAndNewlines).hasSuffix(testString) + + XCTAssertTrue(messageSucceeded ?? false) + XCTAssertEqual(interceptStream.strings.count, 1) + let message = interceptStream.strings.first! + XCTAssertTrue(message.contains("example=example-value"), "message must contain metadata, was: \(message)") + } + func testStdioOutputStreamWrite() { self.withWriteReadFDsAndReadBuffer { writeFD, readFD, readBuffer in let logStream = StdioOutputStream(file: writeFD, flushMode: .always)