Skip to content

Commit 1385c48

Browse files
authored
Slightly improve the way database queries are represented in logging metadata (#635)
Slightly improve the way database queries are represented in logging metadata. This has no visible impact when using the "default" StreamLogHandler, but it's quite visible with smarter log handlers.
1 parent 0f1b8bf commit 1385c48

File tree

4 files changed

+35
-4
lines changed

4 files changed

+35
-4
lines changed

Sources/FluentKit/Query/Database/DatabaseQuery+Range.swift

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,15 @@ extension DatabaseQuery.Limit: CustomStringConvertible {
3131
}
3232
}
3333

34+
extension DatabaseQuery.Limit {
35+
var describedByLoggingMetadata: Logger.MetadataValue {
36+
switch self {
37+
case .count(let count): .stringConvertible(count)
38+
default: "custom"
39+
}
40+
}
41+
}
42+
3443
extension DatabaseQuery.Offset: CustomStringConvertible {
3544
public var description: String {
3645
switch self {
@@ -41,3 +50,12 @@ extension DatabaseQuery.Offset: CustomStringConvertible {
4150
}
4251
}
4352
}
53+
54+
extension DatabaseQuery.Offset {
55+
var describedByLoggingMetadata: Logger.MetadataValue {
56+
switch self {
57+
case .count(let count): .stringConvertible(count)
58+
default: "custom"
59+
}
60+
}
61+
}

Sources/FluentKit/Query/Database/DatabaseQuery+Value.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,23 @@ extension DatabaseQuery.Value: CustomStringConvertible {
3636

3737
var describedByLoggingMetadata: Logger.MetadataValue {
3838
switch self {
39+
case .bind(let encodable):
40+
// N.B.: `is` is used instead of `as?` here because the latter irreversibly loses the `Sendable`-ness of the value.
41+
if encodable is any CustomStringConvertible {
42+
.stringConvertible(encodable as! any CustomStringConvertible & Sendable)
43+
} else {
44+
.string(String(describing: encodable))
45+
}
3946
case .dictionary(let d):
4047
.dictionary(.init(uniqueKeysWithValues: d.map { ($0.description, $1.describedByLoggingMetadata) }))
4148
case .array(let a):
4249
.array(a.map(\.describedByLoggingMetadata))
50+
case .enumCase(let string):
51+
.string(string)
52+
case .null:
53+
"nil"
54+
case .default:
55+
"<default>"
4356
default:
4457
.stringConvertible(self)
4558
}

Sources/FluentKit/Query/Database/DatabaseQuery.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,15 +72,15 @@ extension DatabaseQuery: CustomStringConvertible {
7272
}
7373
switch self.action {
7474
case .read, .aggregate, .custom:
75-
result["unique"] = "\(self.isUnique)"
75+
result["unique"] = .stringConvertible(self.isUnique)
7676
result["fields"] = .array(self.fields.map(\.describedByLoggingMetadata))
7777
result["joins"] = .array(self.joins.map(\.describedByLoggingMetadata))
7878
fallthrough
7979
case .update, .delete:
8080
result["filters"] = .array(self.filters.map(\.describedByLoggingMetadata))
8181
result["sorts"] = .array(self.sorts.map(\.describedByLoggingMetadata))
82-
result["limits"] = .array(self.limits.map { .stringConvertible($0) })
83-
result["offsets"] = .array(self.offsets.map { .stringConvertible($0) })
82+
result["limits"] = .array(self.limits.map(\.describedByLoggingMetadata))
83+
result["offsets"] = .array(self.offsets.map(\.describedByLoggingMetadata))
8484
default: break
8585
}
8686
return result

Tests/FluentKitTests/TestUtilities.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func assertLastQuery(
3939
}
4040

4141
func env(_ name: String) -> String? {
42-
return ProcessInfo.processInfo.environment[name]
42+
ProcessInfo.processInfo.environment[name]
4343
}
4444

4545
let isLoggingConfigured: Bool = {

0 commit comments

Comments
 (0)