Skip to content

Commit 503f10a

Browse files
Add init which accepts LoggerProtocol. Generally cleanup logger config.
1 parent 45750cc commit 503f10a

File tree

6 files changed

+81
-76
lines changed

6 files changed

+81
-76
lines changed

CHANGELOG.md

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,19 @@
55
* Added the ability to log PowerSync sync network requests.
66

77
```swift
8-
struct InlineLogger: SyncRequestLogger {
9-
func log(_ message: String) {
10-
print("Network: \(message)")
11-
}
12-
}
13-
14-
try await db.connect(
15-
connector: connector,
16-
options: ConnectOptions(
17-
clientConfiguration: SyncClientConfiguration(
18-
requestLogger: SyncRequestLoggerConfiguration(
19-
logLevel: .headers,
20-
logger: InlineLogger()
21-
)
22-
)
23-
)
24-
)
8+
try await database.connect(
9+
connector: Connector(),
10+
options: ConnectOptions(
11+
clientConfiguration: SyncClientConfiguration(
12+
requestLogger: SyncRequestLoggerConfiguration(
13+
requestLevel: .headers
14+
) { message in
15+
// Handle Network request logs here
16+
print(message)
17+
}
18+
)
19+
)
20+
)
2521
```
2622
## 1.3.1
2723

Demo/PowerSyncExample/PowerSync/SystemManager.swift

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,6 @@ func getAttachmentsDirectoryPath() throws -> String {
1313

1414
let logTag = "SystemManager"
1515

16-
struct InlineLogger: SyncRequestLogger {
17-
func log(_ message: String) {
18-
print("Network: \(message)")
19-
}
20-
}
21-
2216
@Observable
2317
class SystemManager {
2418
let connector = SupabaseConnector()
@@ -81,9 +75,10 @@ class SystemManager {
8175
options: ConnectOptions(
8276
clientConfiguration: SyncClientConfiguration(
8377
requestLogger: SyncRequestLoggerConfiguration(
84-
logLevel: .headers,
85-
logger: InlineLogger()
86-
)
78+
requestLevel: .headers
79+
) { message in
80+
self.db.logger.debug(message, tag: "SyncRequest")
81+
}
8782
)
8883
)
8984
)

Sources/PowerSync/Kotlin/KotlinNetworkLogger.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ extension SyncRequestLogLevel {
2020
extension SyncRequestLoggerConfiguration {
2121
func toKotlinConfig() -> SwiftRequestLoggerConfig {
2222
return SwiftRequestLoggerConfig(
23-
logLevel: self.logLevel.toKotlin(),
24-
log: { [logger] message in
25-
logger.log(message)
23+
logLevel: self.requestLevel.toKotlin(),
24+
log: { [log] message in
25+
log(message)
2626
}
2727
)
2828
}

Sources/PowerSync/Protocol/PowerSyncDatabaseProtocol.swift

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,7 @@ public struct SyncClientConfiguration {
88
/// Optional configuration for logging PowerSync HTTP requests.
99
///
1010
/// When provided, network requests will be logged according to the
11-
/// specified `SyncRequestLogLevel`. The `logLevel` is set during initialization
12-
/// and remains constant throughout the PowerSync session.
13-
///
14-
/// Set to `nil` to disable request logging entirely.
11+
/// specified `SyncRequestLoggerConfiguration`. Set to `nil` to disable request logging entirely.
1512
///
1613
/// - SeeAlso: `SyncRequestLoggerConfiguration` for configuration options
1714
public let requestLogger: SyncRequestLoggerConfiguration?
Lines changed: 53 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,3 @@
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-
121
/// Level of logs to expose to a `SyncRequestLogger` handler.
132
///
143
/// Controls the verbosity of network logging for PowerSync HTTP requests.
@@ -30,25 +19,65 @@ public enum SyncRequestLogLevel {
3019
/// Configuration for PowerSync HTTP request logging.
3120
///
3221
/// 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.
3524
///
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.
3726
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.
3928
/// Set once during initialization and used throughout the session.
40-
public let logLevel: SyncRequestLogLevel
29+
public let requestLevel: SyncRequestLogLevel
4130

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
4532

4633
/// Creates a new network logger configuration.
4734
/// - 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+
}
5382
}
5483
}

Tests/PowerSyncTests/ConnectTests.swift

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -96,21 +96,6 @@ final class ConnectTests: XCTestCase {
9696

9797
let fakeUrl = "https://fakepowersyncinstance.fakepowersync.local"
9898

99-
struct InlineLogger: SyncRequestLogger {
100-
let logger: (_: String) -> Void
101-
102-
func log(_ message: String) {
103-
logger(message)
104-
}
105-
}
106-
107-
let testLogger = InlineLogger { message in
108-
// We want to see a request to the specified instance
109-
if message.contains(fakeUrl) {
110-
expectation.fulfill()
111-
}
112-
}
113-
11499
class TestConnector: PowerSyncBackendConnector {
115100
let url: String
116101

@@ -131,10 +116,13 @@ final class ConnectTests: XCTestCase {
131116
options: ConnectOptions(
132117
clientConfiguration: SyncClientConfiguration(
133118
requestLogger: SyncRequestLoggerConfiguration(
134-
logLevel:
135-
.all,
136-
logger: testLogger
137-
)
119+
requestLevel: .all
120+
) { message in
121+
// We want to see a request to the specified instance
122+
if message.contains(fakeUrl) {
123+
expectation.fulfill()
124+
}
125+
}
138126
)
139127
)
140128
)

0 commit comments

Comments
 (0)