Skip to content

Commit e9160a3

Browse files
committed
Support custom loggers with GRDB pool
1 parent 5e95f96 commit e9160a3

File tree

3 files changed

+47
-3
lines changed

3 files changed

+47
-3
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## 1.13.0 (unreleased)
4+
5+
* Add optional `logger` parameter on `openPowerSyncWithGRDB` to enable custom loggers.
6+
37
## 1.12.0
48

59
* Make raw tables easier to use:

Sources/PowerSyncGRDB/GRDBPowerSyncDatabase.swift

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,17 +38,20 @@ import PowerSync
3838
/// - pool: The GRDB DatabasePool instance to use for storage
3939
/// - schema: The PowerSync schema describing your sync views
4040
/// - identifier: A unique identifier for this database instance
41+
/// - logger: An optional logger used for the PowerSync database.
4142
/// - Returns: A PowerSync database that works with the provided GRDB pool
4243
public func openPowerSyncWithGRDB(
4344
pool: DatabasePool,
4445
schema: Schema,
45-
identifier: String
46+
identifier: String,
47+
logger: (any LoggerProtocol) = DefaultLogger()
4648
) -> PowerSyncDatabaseProtocol {
4749
return OpenedPowerSyncDatabase(
4850
schema: schema,
4951
pool: GRDBConnectionPool(
5052
pool: pool
5153
),
52-
identifier: identifier
54+
identifier: identifier,
55+
logger: logger
5356
)
5457
}

Tests/PowerSyncGRDBTests/BasicTest.swift

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ final class GRDBTests: XCTestCase {
4444
private var database: PowerSyncDatabaseProtocol!
4545
private var schema: Schema!
4646
private var pool: DatabasePool!
47+
private var logs: TestLogWriterAdapter!
4748

4849
override func setUp() async throws {
4950
try await super.setUp()
@@ -83,10 +84,13 @@ final class GRDBTests: XCTestCase {
8384
configuration: config
8485
)
8586

87+
logs = TestLogWriterAdapter()
88+
let logger = DefaultLogger(minSeverity: LogSeverity.debug, writers: [logs])
8689
database = openPowerSyncWithGRDB(
8790
pool: pool,
8891
schema: schema,
89-
identifier: dbIdentifier
92+
identifier: dbIdentifier,
93+
logger: DatabaseLogger(logger)
9094
)
9195

9296
try await database.disconnectAndClear()
@@ -417,4 +421,37 @@ final class GRDBTests: XCTestCase {
417421
await fulfillment(of: [expectation], timeout: 5)
418422
watchTask.cancel()
419423
}
424+
425+
func testCustomLogger() async throws {
426+
try await database.get("SELECT 1", mapper: { row in })
427+
428+
let warningIndex = logs.getLogs().firstIndex(
429+
where: { value in
430+
value.contains("debug: PowerSyncVersion")
431+
}
432+
)
433+
434+
XCTAssert(warningIndex! >= 0)
435+
}
436+
}
437+
438+
final class TestLogWriterAdapter: LogWriterProtocol,
439+
// The shared state is guarded by the DispatchQueue
440+
@unchecked Sendable
441+
{
442+
private let queue = DispatchQueue(label: "TestLogWriterAdapter")
443+
444+
private var logs = [String]()
445+
446+
func getLogs() -> [String] {
447+
queue.sync {
448+
logs
449+
}
450+
}
451+
452+
func log(severity: LogSeverity, message: String, tag: String?) {
453+
queue.sync {
454+
logs.append("\(severity): \(message) \(tag != nil ? "\(tag!)" : "")")
455+
}
456+
}
420457
}

0 commit comments

Comments
 (0)