Skip to content

Commit ce592ae

Browse files
authored
remove superfluous whitespace (#366)
### Motivation: `StreamLogHandler` has at some point in time acquired an extra whitespace: ``` 2025-07-18T10:09:12+0100 notice label : hello ^ | this one is superfluous ``` but we don't typically put spaces in front of `:`s. This PR changes it to ``` 2025-07-18T10:09:12+0100 notice label: hello ^ | no extra space ``` Additionally, if the label is empty (`""`), then we shouldn't print any spaces there. So with an empty label, we go from ``` 2025-07-18T10:09:12+0100 notice : hello ^^ | two superfluous spaces ``` to ``` 2025-07-18T10:09:12+0100 notice: hello ^ | no extra spaces ``` ### Modifications: - Remove superfluous space if label is not empty - Remove two superfluous spaces if label is empty ### Result: - Somewhat more compact output - Nicer output
1 parent e1600be commit ce592ae

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

Sources/Logging/Logging.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1396,7 +1396,7 @@ public struct StreamLogHandler: LogHandler {
13961396

13971397
var stream = self.stream
13981398
stream.write(
1399-
"\(self.timestamp()) \(level) \(self.label) :\(prettyMetadata.map { " \($0)" } ?? "") [\(source)] \(message)\n"
1399+
"\(self.timestamp()) \(level)\(self.label.isEmpty ? "" : " ")\(self.label):\(prettyMetadata.map { " \($0)" } ?? "") [\(source)] \(message)\n"
14001400
)
14011401
}
14021402

Tests/LoggingTests/LoggingTest.swift

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -989,7 +989,31 @@ class LoggingTest: XCTestCase {
989989
log.critical("\(testString)", source: source)
990990

991991
let pattern =
992-
"^\\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)$"
992+
"^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}(\\+|-)\\d{4}\\s\(Logger.Level.critical)\\s\(label):\\s\\[\(source)\\]\\s\(testString)$"
993+
994+
let messageSucceeded =
995+
interceptStream.interceptedText?.trimmingCharacters(in: .whitespacesAndNewlines).range(
996+
of: pattern,
997+
options: .regularExpression
998+
) != nil
999+
1000+
XCTAssertTrue(messageSucceeded)
1001+
XCTAssertEqual(interceptStream.strings.count, 1)
1002+
}
1003+
1004+
func testStreamLogHandlerOutputFormatWithEmptyLabel() {
1005+
let interceptStream = InterceptStream()
1006+
LoggingSystem.bootstrapInternal { label in
1007+
StreamLogHandler(label: label, stream: interceptStream)
1008+
}
1009+
let source = "testSource"
1010+
let log = Logger(label: "")
1011+
1012+
let testString = "my message is better than yours"
1013+
log.critical("\(testString)", source: source)
1014+
1015+
let pattern =
1016+
"^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}(\\+|-)\\d{4}\\s\(Logger.Level.critical):\\s\\[\(source)\\]\\s\(testString)$"
9931017

9941018
let messageSucceeded =
9951019
interceptStream.interceptedText?.trimmingCharacters(in: .whitespacesAndNewlines).range(
@@ -1014,7 +1038,7 @@ class LoggingTest: XCTestCase {
10141038
log.critical("\(testString)", metadata: ["test": "test"], source: source)
10151039

10161040
let pattern =
1017-
"^\\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)$"
1041+
"^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}(\\+|-)\\d{4}\\s\(Logger.Level.critical)\\s\(label):\\stest=test\\s\\[\(source)\\]\\s\(testString)$"
10181042

10191043
let messageSucceeded =
10201044
interceptStream.interceptedText?.trimmingCharacters(in: .whitespacesAndNewlines).range(

0 commit comments

Comments
 (0)