Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update metadata formatting for readability #163

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Sources/Logging/Logging.swift
Original file line number Diff line number Diff line change
Expand Up @@ -711,12 +711,12 @@ public struct StreamLogHandler: LogHandler {
: self.prettify(self.metadata.merging(metadata!, uniquingKeysWith: { _, new in new }))

var stream = self.stream
stream.write("\(self.timestamp()) \(level) \(self.label) :\(prettyMetadata.map { " \($0)" } ?? "") \(message)\n")
stream.write("\(self.timestamp()) \(level) \(self.label) : \(message)\n\(prettyMetadata.map { "\t\($0)\n" } ?? "\n")")
}

private func prettify(_ metadata: Logger.Metadata) -> String? {
return !metadata.isEmpty
? metadata.lazy.sorted(by: { $0.key < $1.key }).map { "\($0)=\($1)" }.joined(separator: " ")
? metadata.lazy.sorted(by: { $0.key < $1.key }).map { "\($0)=\($1)" }.joined(separator: "\n\t")
: nil
}

Expand Down
8 changes: 4 additions & 4 deletions Tests/LoggingTests/LoggingTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -629,9 +629,9 @@ class LoggingTest: XCTestCase {
let testString = "my message is better than yours"
log.critical("\(testString)", metadata: ["test": "test"])

let pattern = "^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}(\\+|-)\\d{4}\\s\(Logger.Level.critical)\\s\(label)\\s:\\stest=test\\s\(testString)$"
let pattern = "^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}(\\+|-)\\d{4}\\s\(Logger.Level.critical)\\s\(label)\\s:\\s\(testString)\\n\\stest=test\\n"

let messageSucceeded = interceptStream.interceptedText?.trimmingCharacters(in: .whitespacesAndNewlines).range(of: pattern, options: .regularExpression) != nil
let messageSucceeded = interceptStream.interceptedText?.trimmingCharacters(in: .whitespaces).range(of: pattern, options: .regularExpression) != nil

XCTAssertTrue(messageSucceeded)
XCTAssertEqual(interceptStream.strings.count, 1)
Expand All @@ -655,8 +655,8 @@ class LoggingTest: XCTestCase {
return
}

XCTAssert(interceptStream.strings[0].contains("a=a0 b=b0"))
XCTAssert(interceptStream.strings[1].contains("a=a1 b=b1"))
XCTAssert(interceptStream.strings[0].contains("a=a0\n\tb=b0"))
XCTAssert(interceptStream.strings[1].contains("a=a1\n\tb=b1"))
}

func testStdioOutputStreamFlush() {
Expand Down