Skip to content

Commit 3763bf6

Browse files
committed
Completely remove jsonCodersForStringConvertibles for Swift <5.7
1 parent 3aa6e8f commit 3763bf6

File tree

1 file changed

+40
-2
lines changed

1 file changed

+40
-2
lines changed

Sources/JSONLogger.swift

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,10 @@ public struct JSONLogger : LogHandler {
4949
return res
5050
}()
5151

52+
#if swift(>=5.7)
5253
public static let defaultJSONCodersForStringConvertibles: (JSONEncoder, JSONDecoder) = {
5354
let encoder = JSONEncoder()
54-
#if swift(>=5.3)
5555
encoder.outputFormatting = [.withoutEscapingSlashes]
56-
#endif
5756
encoder.keyEncodingStrategy = .useDefaultKeys
5857
encoder.dateEncodingStrategy = .iso8601
5958
encoder.dataEncodingStrategy = .base64
@@ -76,6 +75,7 @@ public struct JSONLogger : LogHandler {
7675
decoder.nonConformingFloatDecodingStrategy = .throw
7776
return (encoder, decoder)
7877
}()
78+
#endif
7979

8080
public var logLevel: Logger.Level = .info
8181

@@ -92,11 +92,14 @@ public struct JSONLogger : LogHandler {
9292
public let suffix: Data
9393

9494
public let jsonEncoder: JSONEncoder
95+
#if swift(>=5.7)
9596
/**
9697
If non-`nil`, the `Encodable` stringConvertible properties in the metadata will be encoded as `JSON` using the `JSONEncoder` and `JSONDecoder`.
9798
If the encoding fails or this property is set to `nil` the String value will be used. */
9899
public let jsonCodersForStringConvertibles: (JSONEncoder, JSONDecoder)?
100+
#endif
99101

102+
#if swift(>=5.7)
100103
public static func forJSONSeq(
101104
on fh: FileHandle = .standardOutput,
102105
label: String,
@@ -133,6 +136,41 @@ public struct JSONLogger : LogHandler {
133136
self.metadataProvider = metadataProvider
134137
}
135138

139+
#else
140+
141+
public static func forJSONSeq(
142+
on fh: FileHandle = .standardOutput,
143+
label: String,
144+
jsonEncoder: JSONEncoder = Self.defaultJSONEncoder,
145+
metadataProvider: Logger.MetadataProvider? = LoggingSystem.metadataProvider
146+
) -> Self {
147+
return Self(
148+
label: label,
149+
fileHandle: fh,
150+
lineSeparator: Data(), prefix: Data([0x1e]), suffix: Data([0x0a]),
151+
jsonEncoder: jsonEncoder,
152+
metadataProvider: metadataProvider
153+
)
154+
}
155+
156+
public init(
157+
label: String,
158+
fileHandle: FileHandle = .standardOutput,
159+
lineSeparator: Data = Data(), prefix: Data = Data(), suffix: Data = Data("\n".utf8),
160+
jsonEncoder: JSONEncoder = Self.defaultJSONEncoder,
161+
metadataProvider: Logger.MetadataProvider? = LoggingSystem.metadataProvider
162+
) {
163+
self.label = label
164+
self.outputFileHandle = fileHandle
165+
self.lineSeparator = lineSeparator
166+
self.prefix = prefix
167+
self.suffix = suffix
168+
self.jsonEncoder = jsonEncoder
169+
170+
self.metadataProvider = metadataProvider
171+
}
172+
#endif
173+
136174
public subscript(metadataKey metadataKey: String) -> Logger.Metadata.Value? {
137175
get {metadata[metadataKey]}
138176
set {metadata[metadataKey] = newValue}

0 commit comments

Comments
 (0)