Skip to content

Commit 65f032f

Browse files
committed
Actually use the JSONEncoder given in init of JSONLogger
1 parent 0db4e18 commit 65f032f

File tree

1 file changed

+19
-4
lines changed

1 file changed

+19
-4
lines changed

Sources/JSONLogger.swift

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,13 +82,27 @@ public struct JSONLogger : LogHandler {
8282
public let prefix: Data
8383
public let suffix: Data
8484

85+
public let jsonEncoder: JSONEncoder
8586
/**
8687
If non-`nil`, the `Encodable` stringConvertible properties in the metadata will be encoded as `JSON` using the `JSONEncoder` and `JSONDecoder`.
8788
If the encoding fails or this property is set to `nil` the String value will be used. */
88-
public var jsonCodersForStringConvertibles: (JSONEncoder, JSONDecoder)?
89+
public let jsonCodersForStringConvertibles: (JSONEncoder, JSONDecoder)?
8990

90-
public static func forJSONSeq(on fd: FileDescriptor = .standardError, label: String, metadataProvider: Logger.MetadataProvider? = LoggingSystem.metadataProvider) -> Self {
91-
return Self(label: label, fd: fd, lineSeparator: Data(), prefix: Data([0x1e]), suffix: Data([0x0a]), metadataProvider: metadataProvider)
91+
public static func forJSONSeq(
92+
on fd: FileDescriptor = .standardError,
93+
label: String,
94+
jsonEncoder: JSONEncoder = Self.defaultJSONEncoder,
95+
jsonCodersForStringConvertibles: (JSONEncoder, JSONDecoder) = Self.defaultJSONCodersForStringConvertibles,
96+
metadataProvider: Logger.MetadataProvider? = LoggingSystem.metadataProvider
97+
) -> Self {
98+
return Self(
99+
label: label,
100+
fd: fd,
101+
lineSeparator: Data(), prefix: Data([0x1e]), suffix: Data([0x0a]),
102+
jsonEncoder: jsonEncoder,
103+
jsonCodersForStringConvertibles: jsonCodersForStringConvertibles,
104+
metadataProvider: metadataProvider
105+
)
92106
}
93107

94108
public init(
@@ -104,6 +118,7 @@ public struct JSONLogger : LogHandler {
104118
self.lineSeparator = lineSeparator
105119
self.prefix = prefix
106120
self.suffix = suffix
121+
self.jsonEncoder = jsonEncoder
107122
self.jsonCodersForStringConvertibles = jsonCodersForStringConvertibles
108123

109124
self.metadataProvider = metadataProvider
@@ -123,7 +138,7 @@ public struct JSONLogger : LogHandler {
123138
let line = LogLine(level: level, message: message.description, metadata: effectiveJSONMetadata,
124139
label: label, source: source, file: file, function: function, line: line)
125140
let jsonLine: Data
126-
do {jsonLine = try JSONEncoder().encode(line)}
141+
do {jsonLine = try jsonEncoder.encode(line)}
127142
catch {
128143
/* The encoding should never fail.
129144
* But what if it does? */

0 commit comments

Comments
 (0)