Skip to content

Commit 1c53831

Browse files
authored
Use OSLog in docs and demo schemas (#42)
* Use OSLog in docs and demo schemas * wip
1 parent 16c6f5e commit 1c53831

File tree

3 files changed

+32
-10
lines changed

3 files changed

+32
-10
lines changed

Examples/Reminders/Schema.swift

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import Foundation
22
import IssueReporting
3+
import OSLog
34
import SharingGRDB
45
import SwiftUI
56

@@ -90,14 +91,14 @@ func appDatabase() throws -> any DatabaseWriter {
9091
configuration.prepareDatabase { db in
9192
#if DEBUG
9293
db.trace(options: .profile) {
93-
print($0.expandedDescription)
94+
logger.debug("\($0.expandedDescription)")
9495
}
9596
#endif
9697
}
9798
@Dependency(\.context) var context
9899
if context == .live {
99100
let path = URL.documentsDirectory.appending(component: "db.sqlite").path()
100-
print("open", path)
101+
logger.info("open \(path)")
101102
database = try DatabasePool(path: path, configuration: configuration)
102103
} else {
103104
database = try DatabaseQueue(configuration: configuration)
@@ -172,6 +173,8 @@ func appDatabase() throws -> any DatabaseWriter {
172173
return database
173174
}
174175

176+
private let logger = Logger(subsystem: "Reminders", category: "Database")
177+
175178
#if DEBUG
176179
extension Database {
177180
func seedSampleData() throws {

Examples/SyncUps/Schema.swift

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import OSLog
12
import SharingGRDB
23
import SwiftUI
34

@@ -82,14 +83,14 @@ func appDatabase() throws -> any DatabaseWriter {
8283
configuration.prepareDatabase { db in
8384
#if DEBUG
8485
db.trace(options: .profile) {
85-
print($0.expandedDescription)
86+
logger.debug("\($0.expandedDescription)")
8687
}
8788
#endif
8889
}
8990
@Dependency(\.context) var context
9091
if context == .live {
9192
let path = URL.documentsDirectory.appending(component: "db.sqlite").path()
92-
print("open", path)
93+
logger.info("open \(path)")
9394
database = try DatabasePool(path: path, configuration: configuration)
9495
} else {
9596
database = try DatabaseQueue(configuration: configuration)
@@ -154,6 +155,8 @@ func appDatabase() throws -> any DatabaseWriter {
154155
return database
155156
}
156157

158+
private let logger = Logger(subsystem: "SyncUps", category: "Database")
159+
157160
extension Database {
158161
fileprivate func seedSampleData() throws {
159162
try seed {

Sources/SharingGRDBCore/Documentation.docc/Articles/PreparingDatabase.md

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,23 +57,30 @@ than you expect. We also recommend only doing this in debug builds to avoid leak
5757
information when the app is running on a user's device:
5858

5959
```diff
60+
+import OSLog
61+
6062
func appDatabase() -> any DatabaseWriter {
6163
var configuration = Configuration()
6264
configuration.foreignKeysEnabled = true
6365
+ #if DEBUG
6466
+ configuration.prepareDatabase { db in
6567
+ db.trace(options: .profile) {
66-
+ print($0.expandedDescription)
68+
+ logger.debug("\($0.expandedDescription)")
6769
+ }
6870
+ }
6971
+ #endif
7072
}
73+
74+
+private let logger = Logger(subsystem: "MyApp", category: "Database")
7175
```
7276

7377
> Note: `expandedDescription` will also print the data bound to the SQL statement, which can include
74-
> sensitive data that you may not want to leak. In this case we feel it is ok because everything
78+
> sensitive data that you may not want to leak. In this case we feel it is OK because everything
7579
> is surrounded in `#if DEBUG`, but it is something to be careful of in your own apps.
7680
81+
> Tip: OSLog allows you to more flexibly filter logs in Xcode, but if you are on a non-Apple
82+
> platform you can use Swift's `print` function, instead.
83+
7784
For more information on configuring tracing, see [GRDB's documentation][trace-docs] on the
7885
matter.
7986

@@ -93,11 +100,12 @@ way to do this is to construct the database connection for a path on the file sy
93100
#if DEBUG
94101
configuration.prepareDatabase { db in
95102
db.trace(options: .profile) {
96-
print($0.expandedDescription)
103+
logger.debug("\($0.expandedDescription)")
97104
}
98105
}
99106
#endif
100107
+ let path = URL.documentsDirectory.appending(component: "db.sqlite").path()
108+
+ logger.info("open \(path)")
101109
+ let database = try DatabasePool(path: path, configuration: configuration)
102110
+ return database
103111
}
@@ -117,16 +125,18 @@ context or if we're in a preview or test.
117125
#if DEBUG
118126
configuration.prepareDatabase { db in
119127
db.trace(options: .profile) {
120-
print($0.expandedDescription)
128+
logger.debug("\($0.expandedDescription)")
121129
}
122130
}
123131
#endif
124132
- let path = URL.documentsDirectory.appending(component: "db.sqlite").path()
133+
- logger.info("open \(path)")
125134
- let database = try DatabasePool(path: path, configuration: configuration)
126135
+ @Dependency(\.context) var context
127136
+ let database: any DatabaseWriter
128137
+ if context == .live {
129138
+ let path = URL.documentsDirectory.appending(component: "db.sqlite").path()
139+
+ logger.info("open \(path)")
130140
+ database = try DatabasePool(path: path, configuration: configuration)
131141
+ } else {
132142
+ database = try DatabaseQueue(configuration: configuration)
@@ -154,14 +164,15 @@ database connection:
154164
#if DEBUG
155165
configuration.prepareDatabase { db in
156166
db.trace(options: .profile) {
157-
print($0.expandedDescription)
167+
logger.debug("\($0.expandedDescription)")
158168
}
159169
}
160170
#endif
161171
@Dependency(\.context) var context
162172
let database: any DatabaseWriter
163173
if context == .live {
164174
let path = URL.documentsDirectory.appending(component: "db.sqlite").path()
175+
logger.info("open \(path)")
165176
database = try DatabasePool(path: path, configuration: configuration)
166177
} else {
167178
database = try DatabaseQueue(configuration: configuration)
@@ -187,20 +198,23 @@ That is all it takes to create, configure and migrate a database connection. Her
187198
we have just written in one snippet:
188199

189200
```swift
201+
import OSLog
202+
190203
func appDatabase() throws -> any DatabaseWriter {
191204
var configuration = Configuration()
192205
configuration.foreignKeysEnabled = true
193206
#if DEBUG
194207
configuration.prepareDatabase { db in
195208
db.trace(options: .profile) {
196-
print($0.expandedDescription)
209+
logger.debug("\($0.expandedDescription)")
197210
}
198211
}
199212
#endif
200213
@Dependency(\.context) var context
201214
let database: any DatabaseWriter
202215
if context == .live {
203216
let path = URL.documentsDirectory.appending(component: "db.sqlite").path()
217+
logger.info("open \(path)")
204218
database = try DatabasePool(path: path, configuration: configuration)
205219
} else {
206220
database = try DatabaseQueue(configuration: configuration)
@@ -218,6 +232,8 @@ func appDatabase() throws -> any DatabaseWriter {
218232
try migrator.migrate(database)
219233
return database
220234
}
235+
236+
private let logger = Logger(subsystem: "MyApp", category: "Database")
221237
```
222238

223239
[grdb-migration-docs]: https://swiftpackageindex.com/groue/grdb.swift/master/documentation/grdb/migrations

0 commit comments

Comments
 (0)