Skip to content

Commit 0e2cad7

Browse files
Added loggers to the send methods
1 parent e6cfdb4 commit 0e2cad7

File tree

1 file changed

+30
-16
lines changed

1 file changed

+30
-16
lines changed

Sources/WebPush/WebPushManager.swift

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ 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-
/// - 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.
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 and metadata.
6161
/// - eventLoopGroupProvider: The event loop to use for the internal HTTP client.
6262
public init(
6363
vapidConfiguration: VAPID.Configuration,
@@ -241,11 +241,13 @@ public actor WebPushManager: Sendable {
241241
/// - subscriber: The subscriber to send the push message to.
242242
/// - expiration: The expiration of the push message, after wich delivery will no longer be attempted.
243243
/// - urgency: The urgency of the delivery of the push message.
244+
/// - logger: The logger to use for status updates. If not provided, the background activity logger will be used instead. When running in a server environment, your contextual logger should be used instead giving you full control of logging and metadata.
244245
public func send(
245246
data message: some DataProtocol,
246247
to subscriber: some SubscriberProtocol,
247248
expiration: Expiration = .recommendedMaximum,
248-
urgency: Urgency = .high
249+
urgency: Urgency = .high,
250+
logger: Logger? = nil
249251
) async throws {
250252
switch executor {
251253
case .httpClient(let httpClient):
@@ -254,7 +256,8 @@ public actor WebPushManager: Sendable {
254256
data: message,
255257
subscriber: subscriber,
256258
expiration: expiration,
257-
urgency: urgency
259+
urgency: urgency,
260+
logger: logger ?? backgroundActivityLogger
258261
)
259262
case .handler(let handler):
260263
try await handler(.data(Data(message)), Subscriber(subscriber), expiration, urgency)
@@ -270,17 +273,20 @@ public actor WebPushManager: Sendable {
270273
/// - subscriber: The subscriber to send the push message to.
271274
/// - expiration: The expiration of the push message, after wich delivery will no longer be attempted.
272275
/// - urgency: The urgency of the delivery of the push message.
276+
/// - logger: The logger to use for status updates. If not provided, the background activity logger will be used instead. When running in a server environment, your contextual logger should be used instead giving you full control of logging and metadata.
273277
public func send(
274278
string message: some StringProtocol,
275279
to subscriber: some SubscriberProtocol,
276280
expiration: Expiration = .recommendedMaximum,
277-
urgency: Urgency = .high
281+
urgency: Urgency = .high,
282+
logger: Logger? = nil
278283
) async throws {
279284
try await routeMessage(
280285
message: .string(String(message)),
281286
to: subscriber,
282287
expiration: expiration,
283-
urgency: urgency
288+
urgency: urgency,
289+
logger: logger ?? backgroundActivityLogger
284290
)
285291
}
286292

@@ -293,17 +299,20 @@ public actor WebPushManager: Sendable {
293299
/// - subscriber: The subscriber to send the push message to.
294300
/// - expiration: The expiration of the push message, after wich delivery will no longer be attempted.
295301
/// - urgency: The urgency of the delivery of the push message.
302+
/// - logger: The logger to use for status updates. If not provided, the background activity logger will be used instead. When running in a server environment, your contextual logger should be used instead giving you full control of logging and metadata.
296303
public func send(
297304
json message: some Encodable&Sendable,
298305
to subscriber: some SubscriberProtocol,
299306
expiration: Expiration = .recommendedMaximum,
300-
urgency: Urgency = .high
307+
urgency: Urgency = .high,
308+
logger: Logger? = nil
301309
) async throws {
302310
try await routeMessage(
303311
message: .json(message),
304312
to: subscriber,
305313
expiration: expiration,
306-
urgency: urgency
314+
urgency: urgency,
315+
logger: logger ?? backgroundActivityLogger
307316
)
308317
}
309318

@@ -313,11 +322,13 @@ public actor WebPushManager: Sendable {
313322
/// - subscriber: The subscriber to sign the message against.
314323
/// - expiration: The expiration of the message.
315324
/// - urgency: The urgency of the message.
325+
/// - logger: The logger to use for status updates.
316326
func routeMessage(
317327
message: _Message,
318328
to subscriber: some SubscriberProtocol,
319329
expiration: Expiration,
320-
urgency: Urgency
330+
urgency: Urgency,
331+
logger: Logger
321332
) async throws {
322333
switch executor {
323334
case .httpClient(let httpClient):
@@ -326,7 +337,8 @@ public actor WebPushManager: Sendable {
326337
data: message.data,
327338
subscriber: subscriber,
328339
expiration: expiration,
329-
urgency: urgency
340+
urgency: urgency,
341+
logger: logger
330342
)
331343
case .handler(let handler):
332344
try await handler(
@@ -345,16 +357,18 @@ public actor WebPushManager: Sendable {
345357
/// - subscriber: The subscriber to sign the message against.
346358
/// - expiration: The expiration of the message.
347359
/// - urgency: The urgency of the message.
360+
/// - logger: The logger to use for status updates.
348361
func execute(
349362
httpClient: some HTTPClientProtocol,
350363
data message: some DataProtocol,
351364
subscriber: some SubscriberProtocol,
352365
expiration: Expiration,
353-
urgency: Urgency
366+
urgency: Urgency,
367+
logger: Logger
354368
) async throws {
355369
guard let signingKey = vapidKeyLookup[subscriber.vapidKeyID]
356370
else {
357-
backgroundActivityLogger.warning("A key was not found for this subscriber.", metadata: [
371+
logger.warning("A key was not found for this subscriber.", metadata: [
358372
"vapidKeyID": "\(subscriber.vapidKeyID)"
359373
])
360374
throw VAPID.ConfigurationError.matchingKeyNotFound
@@ -374,7 +388,7 @@ public actor WebPushManager: Sendable {
374388
for index in salt.indices { salt[index] = .random(in: .min ... .max) }
375389

376390
if message.count > Self.maximumMessageSize {
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)"])
391+
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)"])
378392
}
379393

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

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

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

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

445459
/// Check the response and determine if the subscription should be removed from our records, or if the notification should just be skipped.
446460
switch response.status {
@@ -450,7 +464,7 @@ public actor WebPushManager: Sendable {
450464
// TODO: 429 too many requests, 500 internal server error, 503 server shutting down - check config and perform a retry after a delay?
451465
default: throw HTTPError(response: response)
452466
}
453-
backgroundActivityLogger.trace("Sent \(message) notification to \(subscriber): \(response)")
467+
logger.trace("Sent \(message) notification to \(subscriber): \(response)")
454468
}
455469
}
456470

0 commit comments

Comments
 (0)