Skip to content

Commit de582f8

Browse files
Updated main logger logic to use a fallback logger rather than a child logger
1 parent 32eb2bf commit de582f8

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

Sources/WebPush/WebPushManager.swift

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public actor WebPushManager: Sendable {
3636
/// This is currently set to 3,993 plaintext bytes. See the discussion for ``maximumEncryptedPayloadSize`` for more information.
3737
public static let maximumMessageSize = maximumEncryptedPayloadSize - 103
3838

39-
/// The internal logger to use when reporting status.
39+
/// The internal logger to use when reporting misconfiguration and background activity.
4040
nonisolated let logger: Logger
4141

4242
/// The internal executor to use when delivering messages.
@@ -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: An optional parent logger to use for status updates.
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.
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? = nil,
65+
logger: Logger? = .defaultWebPushPrintLogger,
6666
eventLoopGroupProvider: NIOEventLoopGroupProvider = .shared(.singletonMultiThreadedEventLoopGroup)
6767
) {
68-
let logger = Logger(label: "WebPushManager", factory: { logger?.handler ?? PrintLogHandler(label: $0, metadataProvider: $1) })
68+
let logger = logger ?? .defaultWebPushNoOpLogger
6969

7070
var httpClientConfiguration = HTTPClient.Configuration()
7171
httpClientConfiguration.httpVersion = .automatic
@@ -676,3 +676,15 @@ extension WebPushManager {
676676
) async throws -> Void)
677677
}
678678
}
679+
680+
extension Logger {
681+
/// A logger that will print logs by default.
682+
///
683+
/// This is used by ``WebPushManager/init(vapidConfiguration:logger:eventLoopGroupProvider:)`` to provide a default logger when one is not provided.
684+
public static let defaultWebPushPrintLogger = Logger(label: "WebPushManager", factory: { PrintLogHandler(label: $0, metadataProvider: $1) })
685+
686+
/// A logger that will not print anything by default.
687+
///
688+
/// This is used by ``WebPushManager/init(vapidConfiguration:logger:eventLoopGroupProvider:)`` to provide a default logger when nil is specified.
689+
public static let defaultWebPushNoOpLogger = Logger(label: "WebPushManager", factory: { _, _ in SwiftLogNoOpLogHandler() })
690+
}

0 commit comments

Comments
 (0)