Skip to content

Commit d2db183

Browse files
committed
Change the default config when creating a JSONLogger
1 parent fb3fb3e commit d2db183

File tree

2 files changed

+11
-12
lines changed

2 files changed

+11
-12
lines changed

Sources/JSONLogger.swift

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,25 +13,24 @@ import Logging
1313
/**
1414
A logger that logs it’s messages to stdout in the JSON format, one log per line.
1515

16-
The line separator is actually customizable, and can be any sequence of bytes.
16+
The end of line separator is actually customizable, and can be any sequence of bytes.
1717
By default it’s “`\n`”.
1818

1919
The separator customization allows you to choose
2020
a prefix for the JSON payload (defaults to `[]`),
21-
a suffix too (defaults to `[]` too),
22-
and an inter-JSON separator (defaults to `[0x0a]`, which is a UNIX newline).
21+
a suffix too (defaults to `[0x0a]`, aka. a single UNIX newline),
22+
and an inter-JSON separator (defaults to `[]`, same as the prefix).
2323
For instance if there are two messages logged, you’ll get the following written to the fd:
2424
```
2525
prefix JSON1 suffix separator prefix JSON2 suffix
2626
```
2727

28-
This configuration is interesting mostly to generate `json-seq` stream.
29-
To do this, set the inter-JSON separator to `[]`, the prefix to `[0x1e]` and the suffix to `[0x0a]`,
30-
or use the convenience ``forJSONSeq(on:label:metadataProvider:)``.
28+
An interesting configuration is setting the prefix to `[0x1e]` and the suffix to `[0x0a]`, which generates a `json-seq` stream.
29+
You can use the ``forJSONSeq(on:label:metadataProvider:)`` convenience to get this configuration directly.
3130

32-
Finally, another interesting configuration is to set the separator to `[0xff]` or `[0xfe]`.
31+
Another interesting configuration is to set the inter-JSON separator to `[0xff]` or `[0xfe]` (or both).
3332
These bytes should not appear in valid UTF-8 strings and should be able to be used to separate JSON payloads.
34-
(Note I’m not sure why `json-seq` does not do that; there must be a good reason, though.
33+
(Note I’m not sure why `json-seq` does not do that but there must be a good reason.
3534
Probably because the resulting output would not be valid UTF-8 anymore.)
3635

3736
The output file descriptor is also customizable and is `stdout` by default. */
@@ -113,7 +112,7 @@ public struct JSONLogger : LogHandler {
113112
public init(
114113
label: String,
115114
fd: FileDescriptor = .standardOutput,
116-
lineSeparator: Data = Data("\n".utf8), prefix: Data = Data(), suffix: Data = Data(),
115+
lineSeparator: Data = Data(), prefix: Data = Data(), suffix: Data = Data("\n".utf8),
117116
jsonEncoder: JSONEncoder = Self.defaultJSONEncoder,
118117
jsonCodersForStringConvertibles: (JSONEncoder, JSONDecoder) = Self.defaultJSONCodersForStringConvertibles,
119118
metadataProvider: Logger.MetadataProvider? = LoggingSystem.metadataProvider

Tests/JSONLoggerTests.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,12 @@ final class JSONLoggerTests: XCTestCase {
4949
XCTAssertEqual("second", logger2[metadataKey: "only-on"])
5050
}
5151

52-
/* Must be the first test. */
52+
/* ⚠️ Must be the first test. */
5353
func test0NoSeparatorForFirstLog() throws {
5454
/* We do not init the JSONLogger using Logger because we want to test multiple configurations
5555
* which is not possible using LoggingSystem as the bootstrap can only be done once. */
5656
let pipe = Pipe()
57-
let jsonLogger = JSONLogger(label: "best-logger", fd: FileDescriptor(rawValue: pipe.fileHandleForWriting.fileDescriptor))
57+
let jsonLogger = JSONLogger(label: "best-logger", fd: FileDescriptor(rawValue: pipe.fileHandleForWriting.fileDescriptor), lineSeparator: Data([0x0a]), prefix: Data(), suffix: Data())
5858
jsonLogger.log(level: .info, message: "First log message", metadata: nil, source: "dummy-source", file: "dummy-file", function: "dummy-function", line: 42)
5959
try pipe.fileHandleForWriting.close()
6060
let data = try pipe.fileHandleForReading.readToEnd() ?? Data()
@@ -63,7 +63,7 @@ final class JSONLoggerTests: XCTestCase {
6363

6464
func testSeparatorForNotFirstLog() throws {
6565
let pipe = Pipe()
66-
let jsonLogger = JSONLogger(label: "best-logger", fd: FileDescriptor(rawValue: pipe.fileHandleForWriting.fileDescriptor))
66+
let jsonLogger = JSONLogger(label: "best-logger", fd: FileDescriptor(rawValue: pipe.fileHandleForWriting.fileDescriptor), lineSeparator: Data([0x0a]), prefix: Data(), suffix: Data())
6767
jsonLogger.log(level: .info, message: "Not first log message", metadata: nil, source: "dummy-source", file: "dummy-file", function: "dummy-function", line: 42)
6868
try pipe.fileHandleForWriting.close()
6969
let data = try pipe.fileHandleForReading.readToEnd() ?? Data()

0 commit comments

Comments
 (0)