1
- /// A logger which handles PowerSync network request logs.
2
- ///
3
- /// Implement this protocol to receive network request logging messages at the level
4
- /// specified in `SyncRequestLoggerConfiguration`. The `log(_:)` method will be called
5
- /// for each network event that meets the configured logging criteria.
6
- public protocol SyncRequestLogger {
7
- /// Logs a network-related message.
8
- /// - Parameter message: The formatted log message to record
9
- func log( _ message: String )
10
- }
11
-
12
1
/// Level of logs to expose to a `SyncRequestLogger` handler.
13
2
///
14
3
/// Controls the verbosity of network logging for PowerSync HTTP requests.
@@ -30,25 +19,65 @@ public enum SyncRequestLogLevel {
30
19
/// Configuration for PowerSync HTTP request logging.
31
20
///
32
21
/// This configuration is set once during initialization and used throughout
33
- /// the PowerSync session. The `logLevel ` determines which network events
34
- /// are logged, while the `logger` handles the actual log output .
22
+ /// the PowerSync session. The `requestLevel ` determines which network events
23
+ /// are logged.
35
24
///
36
- /// - Note: The log level cannot be changed after initialization. A new call to `PowerSyncDatabase.connect` is required to change the level.
25
+ /// - Note: The request levell cannot be changed after initialization. A new call to `PowerSyncDatabase.connect` is required to change the level.
37
26
public struct SyncRequestLoggerConfiguration {
38
- /// The logging level that determines which network events are logged.
27
+ /// The request logging level that determines which network events are logged.
39
28
/// Set once during initialization and used throughout the session.
40
- public let logLevel : SyncRequestLogLevel
29
+ public let requestLevel : SyncRequestLogLevel
41
30
42
- /// The logger instance that receives network request log messages.
43
- /// Must conform to `SyncRequestLogger` protocol.
44
- public let logger : SyncRequestLogger
31
+ private let logHandler : ( _ message: String ) -> Void
45
32
46
33
/// Creates a new network logger configuration.
47
34
/// - Parameters:
48
- /// - logLevel: The `SyncRequestLogLevel` to use for filtering log messages
49
- /// - logger: A `SyncRequestLogger` instance to handle log output
50
- public init ( logLevel: SyncRequestLogLevel , logger: SyncRequestLogger ) {
51
- self . logLevel = logLevel
52
- self . logger = logger
35
+ /// - requestLevel: The `SyncRequestLogLevel` to use for filtering log messages
36
+ /// - logHandler: A closure which handles log messages
37
+ public init (
38
+ requestLevel: SyncRequestLogLevel ,
39
+ logHandler: @escaping ( _ message: String ) -> Void )
40
+ {
41
+ self . requestLevel = requestLevel
42
+ self . logHandler = logHandler
43
+ }
44
+
45
+ public func log( _ message: String ) {
46
+ logHandler ( message)
47
+ }
48
+
49
+ /// Creates a new network logger configuration using a `LoggerProtocol` instance.
50
+ ///
51
+ /// This initializer allows integration with an existing logging framework by adapting
52
+ /// a `LoggerProtocol` to conform to `SyncRequestLogger`. The specified `logSeverity`
53
+ /// controls the severity level at which log messages are recorded. An optional `logTag`
54
+ /// may be used to help categorize logs.
55
+ ///
56
+ /// - Parameters:
57
+ /// - requestLevel: The `SyncRequestLogLevel` to use for filtering which network events are logged.
58
+ /// - logger: An object conforming to `LoggerProtocol` that will receive log messages.
59
+ /// - logSeverity: The severity level to use for all log messages (defaults to `.debug`).
60
+ /// - logTag: An optional tag to include with each log message, for use by the logging backend.
61
+ public init (
62
+ requestLevel: SyncRequestLogLevel ,
63
+ logger: LoggerProtocol ,
64
+ logSeverity: LogSeverity = . debug,
65
+ logTag: String ? = nil )
66
+ {
67
+ self . requestLevel = requestLevel
68
+ self . logHandler = { message in
69
+ switch logSeverity {
70
+ case . debug:
71
+ logger. debug ( message, tag: logTag)
72
+ case . info:
73
+ logger. info ( message, tag: logTag)
74
+ case . warning:
75
+ logger. warning ( message, tag: logTag)
76
+ case . error:
77
+ logger. error ( message, tag: logTag)
78
+ case . fault:
79
+ logger. fault ( message, tag: logTag)
80
+ }
81
+ }
53
82
}
54
83
}
0 commit comments