Skip to content

Commit e6cfdb4

Browse files
Renamed WebPushManager's logger to backgroundActivityLogger to match AsyncHTTPClient's usage
1 parent bb2a146 commit e6cfdb4

File tree

3 files changed

+33
-33
lines changed

3 files changed

+33
-33
lines changed

Sources/WebPush/WebPushManager.swift

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public actor WebPushManager: Sendable {
3737
public static let maximumMessageSize = maximumEncryptedPayloadSize - 103
3838

3939
/// The internal logger to use when reporting misconfiguration and background activity.
40-
nonisolated let logger: Logger
40+
nonisolated let backgroundActivityLogger: Logger
4141

4242
/// The internal executor to use when delivering messages.
4343
var executor: Executor
@@ -57,15 +57,15 @@ public actor WebPushManager: Sendable {
5757
/// - Note: On debug builds, this initializer will assert if VAPID authorization header expiration times are inconsistently set.
5858
/// - Parameters:
5959
/// - vapidConfiguration: The VAPID configuration to use when identifying the application server.
60-
/// - logger: The logger to use for misconfiguration and background activity. By default, a print logger will be used, and if set to `nil`, a no-op logger will be used in release builds. When running in a server environment, your shared logger should be used instead giving you full control of logging.
60+
/// - backgroundActivityLogger: The logger to use for misconfiguration and background activity. By default, a print logger will be used, and if set to `nil`, a no-op logger will be used in release builds. When running in a server environment, your shared logger should be used instead giving you full control of logging.
6161
/// - eventLoopGroupProvider: The event loop to use for the internal HTTP client.
6262
public init(
6363
vapidConfiguration: VAPID.Configuration,
6464
// TODO: Add networkConfiguration for proxy, number of simultaneous pushes, etc…
65-
logger: Logger? = .defaultWebPushPrintLogger,
65+
backgroundActivityLogger: Logger? = .defaultWebPushPrintLogger,
6666
eventLoopGroupProvider: NIOEventLoopGroupProvider = .shared(.singletonMultiThreadedEventLoopGroup)
6767
) {
68-
let logger = logger ?? .defaultWebPushNoOpLogger
68+
let backgroundActivityLogger = backgroundActivityLogger ?? .defaultWebPushNoOpLogger
6969

7070
var httpClientConfiguration = HTTPClient.Configuration()
7171
httpClientConfiguration.httpVersion = .automatic
@@ -75,18 +75,18 @@ public actor WebPushManager: Sendable {
7575
.httpClient(HTTPClient(
7676
eventLoopGroupProvider: .shared(eventLoopGroup),
7777
configuration: httpClientConfiguration,
78-
backgroundActivityLogger: logger
78+
backgroundActivityLogger: backgroundActivityLogger
7979
))
8080
case .createNew:
8181
.httpClient(HTTPClient(
8282
configuration: httpClientConfiguration,
83-
backgroundActivityLogger: logger
83+
backgroundActivityLogger: backgroundActivityLogger
8484
))
8585
}
8686

8787
self.init(
8888
vapidConfiguration: vapidConfiguration,
89-
logger: logger,
89+
backgroundActivityLogger: backgroundActivityLogger,
9090
executor: executor
9191
)
9292
}
@@ -96,21 +96,21 @@ public actor WebPushManager: Sendable {
9696
/// Note that this must be called before ``run()`` is called or the client's syncShutdown won't be called.
9797
/// - Parameters:
9898
/// - vapidConfiguration: The VAPID configuration to use when identifying the application server.
99-
/// - logger: The logger to use for status updates.
99+
/// - backgroundActivityLogger: The logger to use for misconfiguration and background activity.
100100
/// - executor: The executor to use when sending push messages.
101101
package init(
102102
vapidConfiguration: VAPID.Configuration,
103103
// TODO: Add networkConfiguration for proxy, number of simultaneous pushes, etc…
104-
logger: Logger,
104+
backgroundActivityLogger: Logger,
105105
executor: Executor
106106
) {
107107
if vapidConfiguration.validityDuration > vapidConfiguration.expirationDuration {
108108
assertionFailure("The validity duration must be earlier than the expiration duration since it represents when the VAPID Authorization token will be refreshed ahead of it expiring.")
109-
logger.error("The validity duration must be earlier than the expiration duration since it represents when the VAPID Authorization token will be refreshed ahead of it expiring. Run your application server with the same configuration in debug mode to catch this.")
109+
backgroundActivityLogger.error("The validity duration must be earlier than the expiration duration since it represents when the VAPID Authorization token will be refreshed ahead of it expiring. Run your application server with the same configuration in debug mode to catch this.")
110110
}
111111
if vapidConfiguration.expirationDuration > .hours(24) {
112112
assertionFailure("The expiration duration must be less than 24 hours or else push endpoints will reject messages sent to them.")
113-
logger.error("The expiration duration must be less than 24 hours or else push endpoints will reject messages sent to them. Run your application server with the same configuration in debug mode to catch this.")
113+
backgroundActivityLogger.error("The expiration duration must be less than 24 hours or else push endpoints will reject messages sent to them. Run your application server with the same configuration in debug mode to catch this.")
114114
}
115115
precondition(!vapidConfiguration.keys.isEmpty, "VAPID.Configuration must have keys specified. Please report this as a bug with reproduction steps if encountered: https://github.com/mochidev/swift-webpush/issues.")
116116

@@ -120,7 +120,7 @@ public actor WebPushManager: Sendable {
120120
allKeys.map { ($0.id, $0) },
121121
uniquingKeysWith: { first, _ in first }
122122
)
123-
self.logger = logger
123+
self.backgroundActivityLogger = backgroundActivityLogger
124124
self.executor = executor
125125
}
126126

@@ -354,7 +354,7 @@ public actor WebPushManager: Sendable {
354354
) async throws {
355355
guard let signingKey = vapidKeyLookup[subscriber.vapidKeyID]
356356
else {
357-
logger.warning("A key was not found for this subscriber.", metadata: [
357+
backgroundActivityLogger.warning("A key was not found for this subscriber.", metadata: [
358358
"vapidKeyID": "\(subscriber.vapidKeyID)"
359359
])
360360
throw VAPID.ConfigurationError.matchingKeyNotFound
@@ -374,7 +374,7 @@ public actor WebPushManager: Sendable {
374374
for index in salt.indices { salt[index] = .random(in: .min ... .max) }
375375

376376
if message.count > Self.maximumMessageSize {
377-
logger.warning("Push message is longer than the maximum guarantee made by the spec: \(Self.maximumMessageSize) bytes. Sending this message may fail, and its size will be leaked despite being encrypted. Please consider sending less data to keep your communications secure.", metadata: ["message": "\(message)"])
377+
backgroundActivityLogger.warning("Push message is longer than the maximum guarantee made by the spec: \(Self.maximumMessageSize) bytes. Sending this message may fail, and its size will be leaked despite being encrypted. Please consider sending less data to keep your communications secure.", metadata: ["message": "\(message)"])
378378
}
379379

380380
/// Prepare the payload by padding it so the final message is 4KB.
@@ -424,9 +424,9 @@ public actor WebPushManager: Sendable {
424424
let requestContent = contentCodingHeader + encryptedRecord.ciphertext + encryptedRecord.tag
425425

426426
if expiration < Expiration.dropIfUndeliverable {
427-
logger.error("The message expiration must be greater than or equal to \(Expiration.dropIfUndeliverable) seconds.", metadata: ["expiration": "\(expiration)"])
427+
backgroundActivityLogger.error("The message expiration must be greater than or equal to \(Expiration.dropIfUndeliverable) seconds.", metadata: ["expiration": "\(expiration)"])
428428
} else if expiration > Expiration.recommendedMaximum {
429-
logger.warning("The message expiration should be less than \(Expiration.recommendedMaximum) seconds.", metadata: ["expiration": "\(expiration)"])
429+
backgroundActivityLogger.warning("The message expiration should be less than \(Expiration.recommendedMaximum) seconds.", metadata: ["expiration": "\(expiration)"])
430430
}
431431

432432
/// Add the VAPID authorization and corrent content encoding and type.
@@ -440,7 +440,7 @@ public actor WebPushManager: Sendable {
440440
request.body = .bytes(ByteBuffer(bytes: requestContent))
441441

442442
/// Send the request to the push endpoint.
443-
let response = try await httpClient.execute(request, deadline: .distantFuture, logger: logger)
443+
let response = try await httpClient.execute(request, deadline: .distantFuture, logger: backgroundActivityLogger)
444444

445445
/// Check the response and determine if the subscription should be removed from our records, or if the notification should just be skipped.
446446
switch response.status {
@@ -450,28 +450,28 @@ public actor WebPushManager: Sendable {
450450
// TODO: 429 too many requests, 500 internal server error, 503 server shutting down - check config and perform a retry after a delay?
451451
default: throw HTTPError(response: response)
452452
}
453-
logger.trace("Sent \(message) notification to \(subscriber): \(response)")
453+
backgroundActivityLogger.trace("Sent \(message) notification to \(subscriber): \(response)")
454454
}
455455
}
456456

457457
extension WebPushManager: Service {
458458
public func run() async throws {
459-
logger.debug("Starting up WebPushManager")
459+
backgroundActivityLogger.debug("Starting up WebPushManager")
460460
guard !didShutdown else {
461461
assertionFailure("The manager was already shutdown and cannot be started.")
462-
logger.error("The manager was already shutdown and cannot be started. Run your application server in debug mode to catch this.")
462+
backgroundActivityLogger.error("The manager was already shutdown and cannot be started. Run your application server in debug mode to catch this.")
463463
return
464464
}
465465
try await withTaskCancellationOrGracefulShutdownHandler {
466466
try await gracefulShutdown()
467-
} onCancelOrGracefulShutdown: { [logger, executor] in
468-
logger.debug("Shutting down WebPushManager")
467+
} onCancelOrGracefulShutdown: { [backgroundActivityLogger, executor] in
468+
backgroundActivityLogger.debug("Shutting down WebPushManager")
469469
do {
470470
if case let .httpClient(httpClient) = executor {
471471
try httpClient.syncShutdown()
472472
}
473473
} catch {
474-
logger.error("Graceful Shutdown Failed", metadata: [
474+
backgroundActivityLogger.error("Graceful Shutdown Failed", metadata: [
475475
"error": "\(error)"
476476
])
477477
}

Sources/WebPushTesting/WebPushManager+Testing.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,19 @@ extension WebPushManager {
2525
public static func makeMockedManager(
2626
vapidConfiguration: VAPID.Configuration = .mockedConfiguration,
2727
// TODO: Add networkConfiguration for proxy, number of simultaneous pushes, etc…
28-
logger: Logger? = nil,
28+
backgroundActivityLogger: Logger? = .defaultWebPushPrintLogger,
2929
messageHandler: @escaping @Sendable (
3030
_ message: Message,
3131
_ subscriber: Subscriber,
3232
_ expiration: Expiration,
3333
_ urgency: Urgency
3434
) async throws -> Void
3535
) -> WebPushManager {
36-
let logger = Logger(label: "MockWebPushManager", factory: { logger?.handler ?? PrintLogHandler(label: $0, metadataProvider: $1) })
36+
let backgroundActivityLogger = backgroundActivityLogger ?? .defaultWebPushNoOpLogger
3737

3838
return WebPushManager(
3939
vapidConfiguration: vapidConfiguration,
40-
logger: logger,
40+
backgroundActivityLogger: backgroundActivityLogger,
4141
executor: .handler(messageHandler)
4242
)
4343
}

Tests/WebPushTests/WebPushManagerTests.swift

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ struct WebPushManagerTests {
3333
let logger = Logger(label: "ServiceLogger", factory: { PrintLogHandler(label: $0, metadataProvider: $1) })
3434
let manager = WebPushManager(
3535
vapidConfiguration: .makeTesting(),
36-
logger: logger
36+
backgroundActivityLogger: logger
3737
)
3838
await withThrowingTaskGroup(of: Void.self) { group in
3939
group.addTask {
@@ -217,7 +217,7 @@ struct WebPushManagerTests {
217217

218218
let manager = WebPushManager(
219219
vapidConfiguration: vapidConfiguration,
220-
logger: Logger(label: "WebPushManagerTests", factory: { PrintLogHandler(label: $0, metadataProvider: $1) }),
220+
backgroundActivityLogger: Logger(label: "WebPushManagerTests", factory: { PrintLogHandler(label: $0, metadataProvider: $1) }),
221221
executor: .httpClient(MockHTTPClient({ request in
222222
try validateAuthotizationHeader(
223223
request: request,
@@ -264,7 +264,7 @@ struct WebPushManagerTests {
264264

265265
let manager = WebPushManager(
266266
vapidConfiguration: vapidConfiguration,
267-
logger: Logger(label: "WebPushManagerTests", factory: { PrintLogHandler(label: $0, metadataProvider: $1) }),
267+
backgroundActivityLogger: Logger(label: "WebPushManagerTests", factory: { PrintLogHandler(label: $0, metadataProvider: $1) }),
268268
executor: .httpClient(MockHTTPClient({ request in
269269
try validateAuthotizationHeader(
270270
request: request,
@@ -311,7 +311,7 @@ struct WebPushManagerTests {
311311

312312
let manager = WebPushManager(
313313
vapidConfiguration: vapidConfiguration,
314-
logger: Logger(label: "WebPushManagerTests", factory: { PrintLogHandler(label: $0, metadataProvider: $1) }),
314+
backgroundActivityLogger: Logger(label: "WebPushManagerTests", factory: { PrintLogHandler(label: $0, metadataProvider: $1) }),
315315
executor: .httpClient(MockHTTPClient({ request in
316316
try validateAuthotizationHeader(
317317
request: request,
@@ -358,7 +358,7 @@ struct WebPushManagerTests {
358358

359359
let manager = WebPushManager(
360360
vapidConfiguration: vapidConfiguration,
361-
logger: Logger(label: "WebPushManagerTests", factory: { PrintLogHandler(label: $0, metadataProvider: $1) }),
361+
backgroundActivityLogger: Logger(label: "WebPushManagerTests", factory: { PrintLogHandler(label: $0, metadataProvider: $1) }),
362362
executor: .httpClient(MockHTTPClient({ request in
363363
requestWasMade()
364364
return HTTPClientResponse(status: .notFound)
@@ -387,7 +387,7 @@ struct WebPushManagerTests {
387387

388388
let manager = WebPushManager(
389389
vapidConfiguration: vapidConfiguration,
390-
logger: Logger(label: "WebPushManagerTests", factory: { PrintLogHandler(label: $0, metadataProvider: $1) }),
390+
backgroundActivityLogger: Logger(label: "WebPushManagerTests", factory: { PrintLogHandler(label: $0, metadataProvider: $1) }),
391391
executor: .httpClient(MockHTTPClient({ request in
392392
requestWasMade()
393393
return HTTPClientResponse(status: .gone)
@@ -416,7 +416,7 @@ struct WebPushManagerTests {
416416

417417
let manager = WebPushManager(
418418
vapidConfiguration: vapidConfiguration,
419-
logger: Logger(label: "WebPushManagerTests", factory: { PrintLogHandler(label: $0, metadataProvider: $1) }),
419+
backgroundActivityLogger: Logger(label: "WebPushManagerTests", factory: { PrintLogHandler(label: $0, metadataProvider: $1) }),
420420
executor: .httpClient(MockHTTPClient({ request in
421421
requestWasMade()
422422
return HTTPClientResponse(status: .internalServerError)

0 commit comments

Comments
 (0)