Skip to content

Commit f188481

Browse files
author
nervenes
committed
make logging optional and update docs
1 parent f70b838 commit f188481

File tree

4 files changed

+41
-33
lines changed

4 files changed

+41
-33
lines changed

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ Furthermore, the group will setup signal listeners for the configured signals an
6262
on each service.
6363

6464
```swift
65+
import ServiceLifecycle
66+
6567
actor FooService: Service {
6668
func run() async throws {
6769
print("FooService starting")
@@ -78,13 +80,12 @@ struct Application {
7880

7981
let serviceGroup = ServiceGroup(
8082
services: [service1, service2],
81-
configuration: .init(gracefulShutdownSignals: [.sigterm]),
82-
logger: logger
83+
gracefulShutdownSignals: [.sigterm]
8384
)
85+
8486
try await serviceGroup.run()
8587
}
8688
}
87-
8889
```
8990

9091
## Security

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

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -77,13 +77,7 @@ struct Application {
7777

7878
let serviceGroup = ServiceGroup(
7979
// 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-
),
80+
services: [fooService, barService]
8781
)
8882

8983
try await serviceGroup.run()
@@ -148,11 +142,8 @@ struct Application {
148142
})
149143

150144
let serviceGroup = ServiceGroup(
151-
configuration: .init(
152-
services: [.init(service: streamingService)],
153-
gracefulShutdownSignals: [.sigterm],
154-
logger: logger
155-
)
145+
services: [streamingService],
146+
gracefulShutdownSignals: [.sigterm]
156147
)
157148

158149
try await serviceGroup.run()
@@ -210,11 +201,8 @@ struct Application {
210201
})
211202

212203
let serviceGroup = ServiceGroup(
213-
configuration: .init(
214-
services: [.init(service: streamingService)],
215-
gracefulShutdownSignals: [.sigterm],
216-
logger: logger
217-
)
204+
services: [streamingService],
205+
gracefulShutdownSignals: [.sigterm]
218206
)
219207

220208
try await serviceGroup.run()
@@ -266,8 +254,7 @@ struct Application {
266254
successTerminationBehavior: .shutdownGracefully,
267255
failureTerminationBehavior: .shutdownGracefully
268256
)
269-
],
270-
logger: logger
257+
]
271258
),
272259
)
273260

Sources/ServiceLifecycle/ServiceGroup.swift

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,19 +76,23 @@ public actor ServiceGroup: Sendable, Service {
7676
services: [any Service],
7777
gracefulShutdownSignals: [UnixSignal] = [],
7878
cancellationSignals: [UnixSignal] = [],
79-
logger: Logger
79+
logger: Logger? = nil
8080
) {
81+
if logger == nil {
82+
LoggingSystem.bootstrap { SwiftLogNoOpLogHandler($0) }
83+
}
84+
8185
let configuration = ServiceGroupConfiguration(
8286
services: services.map { ServiceGroupConfiguration.ServiceConfiguration(service: $0) },
8387
gracefulShutdownSignals: gracefulShutdownSignals,
8488
cancellationSignals: cancellationSignals,
85-
logger: logger
89+
logger: logger ?? .init(label: "")
8690
)
8791

8892
self.init(configuration: configuration)
8993
}
9094

91-
@available(*, deprecated)
95+
@available(*, deprecated, renamed: "init(services:gracefulShutdownSignals:cancellationSignals:logger:)")
9296
public init(
9397
services: [any Service],
9498
configuration: ServiceGroupConfiguration,

Sources/ServiceLifecycle/ServiceGroupConfiguration.swift

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -172,10 +172,14 @@ public struct ServiceGroupConfiguration: Sendable {
172172
/// - logger: The group's logger.
173173
public init(
174174
services: [ServiceConfiguration],
175-
logger: Logger
175+
logger: Logger? = nil
176176
) {
177+
if logger == nil {
178+
LoggingSystem.bootstrap { SwiftLogNoOpLogHandler($0) }
179+
}
180+
177181
self.services = services
178-
self.logger = logger
182+
self.logger = logger ?? .init(label: "")
179183
}
180184

181185
/// Initializes a new ``ServiceGroupConfiguration``.
@@ -189,10 +193,14 @@ public struct ServiceGroupConfiguration: Sendable {
189193
services: [ServiceConfiguration],
190194
gracefulShutdownSignals: [UnixSignal] = [],
191195
cancellationSignals: [UnixSignal] = [],
192-
logger: Logger
196+
logger: Logger? = nil
193197
) {
198+
if logger == nil {
199+
LoggingSystem.bootstrap { SwiftLogNoOpLogHandler($0) }
200+
}
201+
194202
self.services = services
195-
self.logger = logger
203+
self.logger = logger ?? .init(label: "")
196204
self.gracefulShutdownSignals = gracefulShutdownSignals
197205
self.cancellationSignals = cancellationSignals
198206
}
@@ -204,10 +212,14 @@ public struct ServiceGroupConfiguration: Sendable {
204212
/// - logger: The group's logger.
205213
public init(
206214
services: [Service],
207-
logger: Logger
215+
logger: Logger? = nil
208216
) {
217+
if logger == nil {
218+
LoggingSystem.bootstrap { SwiftLogNoOpLogHandler($0) }
219+
}
220+
209221
self.services = Array(services.map { ServiceConfiguration(service: $0) })
210-
self.logger = logger
222+
self.logger = logger ?? .init(label: "")
211223
}
212224

213225
/// Initializes a new ``ServiceGroupConfiguration``.
@@ -221,10 +233,14 @@ public struct ServiceGroupConfiguration: Sendable {
221233
services: [Service],
222234
gracefulShutdownSignals: [UnixSignal] = [],
223235
cancellationSignals: [UnixSignal] = [],
224-
logger: Logger
236+
logger: Logger? = nil
225237
) {
238+
if logger == nil {
239+
LoggingSystem.bootstrap { SwiftLogNoOpLogHandler($0) }
240+
}
241+
226242
self.services = Array(services.map { ServiceConfiguration(service: $0) })
227-
self.logger = logger
243+
self.logger = logger ?? .init(label: "")
228244
self.gracefulShutdownSignals = gracefulShutdownSignals
229245
self.cancellationSignals = cancellationSignals
230246
}

0 commit comments

Comments
 (0)