Skip to content

Commit

Permalink
Emit correct metadata from StreamLogHandler (#251)
Browse files Browse the repository at this point in the history
Co-authored-by: Konrad `ktoso` Malawski <[email protected]>
  • Loading branch information
slashmo and ktoso authored Jan 18, 2023
1 parent bd4519f commit 3e3ef75
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Sources/Logging/Logging.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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? {
Expand Down
1 change: 1 addition & 0 deletions Tests/LoggingTests/LoggingTest+XCTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ extension LoggingTest {
("testStreamLogHandlerOutputFormat", testStreamLogHandlerOutputFormat),
("testStreamLogHandlerOutputFormatWithMetaData", testStreamLogHandlerOutputFormatWithMetaData),
("testStreamLogHandlerOutputFormatWithOrderedMetadata", testStreamLogHandlerOutputFormatWithOrderedMetadata),
("testStreamLogHandlerWritesIncludeMetadataProviderMetadata", testStreamLogHandlerWritesIncludeMetadataProviderMetadata),
("testStdioOutputStreamWrite", testStdioOutputStreamWrite),
("testStdioOutputStreamFlush", testStdioOutputStreamFlush),
("testOverloadingError", testOverloadingError),
Expand Down
18 changes: 18 additions & 0 deletions Tests/LoggingTests/LoggingTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 3e3ef75

Please sign in to comment.