@@ -57,23 +57,30 @@ than you expect. We also recommend only doing this in debug builds to avoid leak
5757information 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+
7784For more information on configuring tracing, see [ GRDB's documentation] [ trace-docs ] on the
7885matter.
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
187198we have just written in one snippet:
188199
189200``` swift
201+ import OSLog
202+
190203func 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