Skip to content

Commit e0bc6ea

Browse files
committed
improve logging
1 parent d4b5ebe commit e0bc6ea

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

Examples/ServiceLifecycle/Sources/Lambda.swift

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,11 @@ struct LambdaFunction {
7676
// See: https://github.com/vapor/postgres-nio/issues/489#issuecomment-2186509773
7777
result = try await timeout(deadline: .seconds(3)) {
7878
// check if table exists
79+
logger.trace("Checking database")
7980
try await prepareDatabase()
8081

8182
// query users
83+
logger.trace("Querying database")
8284
return try await self.queryUsers()
8385
}
8486
} catch {
@@ -94,13 +96,19 @@ struct LambdaFunction {
9496
private func prepareDatabase() async throws {
9597
logger.trace("Preparing to the database")
9698
do {
99+
97100
// initial creation of the table. This will fails if it already exists
101+
logger.trace("Testing if table exists")
98102
try await self.pgClient.query(SQLStatements.createTable)
103+
99104
// it did not fail, it means the table is new and empty
105+
logger.trace("Populate table")
100106
try await self.pgClient.query(SQLStatements.populateTable)
107+
101108
} catch is PSQLError {
102109
// when there is a database error, it means the table or values already existed
103110
// ignore this error
111+
logger.trace("Table exists already")
104112
} catch {
105113
// propagate other errors
106114
throw error
@@ -109,12 +117,11 @@ struct LambdaFunction {
109117

110118
/// Query the database
111119
private func queryUsers() async throws -> [User] {
112-
logger.trace("Querying to the database")
113120
var users: [User] = []
114121
let query = SQLStatements.queryAllUsers
115122
let rows = try await self.pgClient.query(query)
116123
for try await (id, username) in rows.decode((Int, String).self) {
117-
self.logger.trace("Adding \(id) : \(username)")
124+
self.logger.trace("\(id) : \(username)")
118125
users.append(User(id: id, username: username))
119126
}
120127
return users

0 commit comments

Comments
 (0)