Skip to content

Commit 0af4033

Browse files
nervenesFranzBusch
andauthored
Make logging optional and update docs (#189)
Hello! 👋 This PR contains a few changes, notably: - Updating the ``README.md`` to use the new initializer instead of the deprecated one. - Add a ``renamed`` to the deprecated initializer. - Updating the docs to use the simpler initializer. - Make the ``logging`` parameter on the initializers optional, the motivation for this is in cases the user don't or can't do logging, they don't have to construct a logger and bootstrap the logging system with ``SwiftLogNoOpLogHandler``, so this code: ```swift LoggingSystem.bootstrap(SwiftLogNoOpLogHandler.init) let serviceGroup = ServiceGroup( services: [service], cancellationSignals: [.sigint], logger: .init(label: "") ) ``` becomes ```swift let serviceGroup = ServiceGroup( services: [service], cancellationSignals: [.sigint] ) ``` and it also allows the user to do logging outside the ServiceGroup without necessarily doing logging within it. If there is anything to improve, let me know! 😃 *thanks dimi and gwynne for helping along the way and answering my questions :)* --------- Co-authored-by: Franz Busch <[email protected]>
1 parent 0ee8bad commit 0af4033

File tree

3 files changed

+36
-20
lines changed

3 files changed

+36
-20
lines changed

README.md

+7-2
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ Furthermore, the group will setup signal listeners for the configured signals an
6262
on each service.
6363

6464
```swift
65+
import ServiceLifecycle
66+
import Logging
67+
6568
actor FooService: Service {
6669
func run() async throws {
6770
print("FooService starting")
@@ -72,19 +75,21 @@ actor FooService: Service {
7275

7376
@main
7477
struct Application {
78+
static let logger = Logger(label: "Application")
79+
7580
static func main() async throws {
7681
let service1 = FooService()
7782
let service2 = FooService()
7883

7984
let serviceGroup = ServiceGroup(
8085
services: [service1, service2],
81-
configuration: .init(gracefulShutdownSignals: [.sigterm]),
86+
gracefulShutdownSignals: [.sigterm],
8287
logger: logger
8388
)
89+
8490
try await serviceGroup.run()
8591
}
8692
}
87-
8893
```
8994

9095
## Security

Sources/ServiceLifecycle/Docs.docc/How to adopt ServiceLifecycle in applications.md

+28-17
Original file line numberDiff line numberDiff line change
@@ -69,21 +69,21 @@ on services with a lower index. The following example shows how this can be
6969
applied to our `BarService`.
7070

7171
```swift
72+
import ServiceLifecycle
73+
import Logging
74+
7275
@main
7376
struct Application {
77+
static let logger = Logger(label: "Application")
78+
7479
static func main() async throws {
7580
let fooService = FooServer()
7681
let barService = BarService(fooService: fooService)
7782

7883
let serviceGroup = ServiceGroup(
7984
// We are encoding the dependency hierarchy here by listing the fooService first
80-
configuration: .init(
81-
services: [
82-
.init(service: fooService),
83-
.init(service: barService)
84-
],
85-
logger: logger
86-
),
85+
services: [fooService, barService],
86+
logger: logger
8787
)
8888

8989
try await serviceGroup.run()
@@ -115,6 +115,9 @@ A common example of this is for applications that implement streaming
115115
behaviours.
116116

117117
```swift
118+
import ServiceLifecycle
119+
import Logging
120+
118121
struct StreamingService: Service {
119122
struct RequestStream: AsyncSequence { ... }
120123
struct ResponseWriter {
@@ -140,6 +143,8 @@ struct StreamingService: Service {
140143

141144
@main
142145
struct Application {
146+
static let logger = Logger(label: "Application")
147+
143148
static func main() async throws {
144149
let streamingService = StreamingService(streamHandler: { requestStream, responseWriter in
145150
for await request in requestStream {
@@ -148,11 +153,9 @@ struct Application {
148153
})
149154

150155
let serviceGroup = ServiceGroup(
151-
configuration: .init(
152-
services: [.init(service: streamingService)],
153-
gracefulShutdownSignals: [.sigterm],
154-
logger: logger
155-
)
156+
services: [streamingService],
157+
gracefulShutdownSignals: [.sigterm],
158+
logger: logger
156159
)
157160

158161
try await serviceGroup.run()
@@ -177,6 +180,9 @@ and what we want to do is stop the iteration. To do this we can use the
177180
`AsyncSequence`. The updated code looks like this:
178181

179182
```swift
183+
import ServiceLifecycle
184+
import Logging
185+
180186
struct StreamingService: Service {
181187
struct RequestStream: AsyncSequence { ... }
182188
struct ResponseWriter {
@@ -202,6 +208,8 @@ struct StreamingService: Service {
202208

203209
@main
204210
struct Application {
211+
static let logger = Logger(label: "Application")
212+
205213
static func main() async throws {
206214
let streamingService = StreamingService(streamHandler: { requestStream, responseWriter in
207215
for await request in requestStream.cancelOnGracefulShutdown() {
@@ -210,11 +218,9 @@ struct Application {
210218
})
211219

212220
let serviceGroup = ServiceGroup(
213-
configuration: .init(
214-
services: [.init(service: streamingService)],
215-
gracefulShutdownSignals: [.sigterm],
216-
logger: logger
217-
)
221+
services: [streamingService],
222+
gracefulShutdownSignals: [.sigterm],,
223+
logger: logger
218224
)
219225

220226
try await serviceGroup.run()
@@ -251,8 +257,13 @@ sure your telemetry service is gracefully shutdown after your HTTP server
251257
unexpectedly threw from its `run()` method. This setup could look like this:
252258

253259
```swift
260+
import ServiceLifecycle
261+
import Logging
262+
254263
@main
255264
struct Application {
265+
static let logger = Logger(label: "Application")
266+
256267
static func main() async throws {
257268
let telemetryService = TelemetryService()
258269
let httpServer = HTTPServer()

Sources/ServiceLifecycle/ServiceGroup.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public actor ServiceGroup: Sendable, Service {
8888
self.init(configuration: configuration)
8989
}
9090

91-
@available(*, deprecated)
91+
@available(*, deprecated, renamed: "init(services:gracefulShutdownSignals:cancellationSignals:logger:)")
9292
public init(
9393
services: [any Service],
9494
configuration: ServiceGroupConfiguration,

0 commit comments

Comments
 (0)