Skip to content

Commit 370a93f

Browse files
committed
Add new convenience initializer for simple LoggingSystem bootstrap
1 parent c424e13 commit 370a93f

File tree

5 files changed

+37
-3
lines changed

5 files changed

+37
-3
lines changed

Package.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ let package = Package(
1515
.target(name: "JSONLogger", dependencies: [
1616
.product(name: "GenericJSON", package: "generic-json"),
1717
.product(name: "Logging", package: "swift-log"),
18-
], path: "Sources"),
18+
], path: "Sources", exclude: ["JSONLogger+WithSendable.swift"]),
1919
.testTarget(name: "JSONLoggerTests", dependencies: ["JSONLogger"]),
2020
]
2121
)

[email protected]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ let package = Package(
2424
.target(name: "JSONLogger", dependencies: [
2525
.product(name: "GenericJSON", package: "generic-json"),
2626
.product(name: "Logging", package: "swift-log"),
27-
], path: "Sources", swiftSettings: swiftSettings),
27+
], path: "Sources", exclude: ["JSONLogger+NoSendable.swift"], swiftSettings: swiftSettings),
2828
.testTarget(name: "JSONLoggerTests", dependencies: ["JSONLogger"], swiftSettings: swiftSettings),
2929
]
3030
)
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import Foundation
2+
3+
import Logging
4+
5+
6+
7+
extension JSONLogger {
8+
9+
public init(label: String, metadataProvider: Logger.MetadataProvider? = LoggingSystem.metadataProvider) {
10+
/* The fileHandle argument should be present to avoid infinite recursion
11+
* but its value should be the same as the default value of the initializer we’re calling (for API consistency). */
12+
self.init(label: label, fileHandle: .standardOutput, metadataProvider: metadataProvider)
13+
}
14+
15+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import Foundation
2+
3+
import Logging
4+
5+
6+
7+
/* The @Sendable attribute is only available starting at Swift 5.5.
8+
* We make these methods only available starting at Swift 5.8 for our convenience (avoids creating another Package@swift-... file)
9+
* and because for Swift <5.8 the non-@Sendable variants of the methods are available. */
10+
extension JSONLogger {
11+
12+
@Sendable
13+
public init(label: String, metadataProvider: Logger.MetadataProvider? = LoggingSystem.metadataProvider) {
14+
/* The fileHandle argument should be present to avoid infinite recursion
15+
* but its value should be the same as the default value of the initializer we’re calling (for API consistency). */
16+
self.init(label: label, fileHandle: .standardOutput, metadataProvider: metadataProvider)
17+
}
18+
19+
}

Tests/JSONLoggerTests/JSONLoggerTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ final class JSONLoggerTests : XCTestCase {
2929
}()
3030

3131
override class func setUp() {
32-
LoggingSystem.bootstrap{ JSONLogger(label: $0) }
32+
LoggingSystem.bootstrap(JSONLogger.init, metadataProvider: nil)
3333
}
3434

3535
/* From <https://apple.github.io/swift-log/docs/current/Logging/Protocols/LogHandler.html#treat-log-level-amp-metadata-as-values>. */

0 commit comments

Comments
 (0)