Skip to content

Include source in StreamLogHandler output #189

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

Merged
merged 1 commit into from
Apr 6, 2021
Merged
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
2 changes: 1 addition & 1 deletion Sources/Logging/Logging.swift
Original file line number Diff line number Diff line change
Expand Up @@ -917,7 +917,7 @@ 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) :\(prettyMetadata.map { " \($0)" } ?? "") [\(source)] \(message)\n")
}

private func prettify(_ metadata: Logger.Metadata) -> String? {
Expand Down
10 changes: 6 additions & 4 deletions Tests/LoggingTests/LoggingTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -669,12 +669,13 @@ class LoggingTest: XCTestCase {
LoggingSystem.bootstrapInternal { _ in
StreamLogHandler(label: label, stream: interceptStream)
}
let source = "testSource"
let log = Logger(label: label)

let testString = "my message is better than yours"
log.critical("\(testString)")
log.critical("\(testString)", source: source)

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)$"
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\\[\(source)\\]\\s\(testString)$"

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

Expand All @@ -688,12 +689,13 @@ class LoggingTest: XCTestCase {
LoggingSystem.bootstrapInternal { _ in
StreamLogHandler(label: label, stream: interceptStream)
}
let source = "testSource"
let log = Logger(label: label)

let testString = "my message is better than yours"
log.critical("\(testString)", metadata: ["test": "test"])
log.critical("\(testString)", metadata: ["test": "test"], source: source)

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:\\stest=test\\s\\[\(source)\\]\\s\(testString)$"

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

Expand Down