Repository navigation
Make Sure Logs Are Sent To The Right Place #229
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
Tests/FluentPostgresDriverTests/FluentPostgresLoggingTests.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| import FluentKit | ||
| import InMemoryLogging | ||
| import Logging | ||
| import SQLKit | ||
| import Testing | ||
|
|
||
| extension AllSuites { | ||
| @Suite | ||
| struct FluentPostgresLoggingTests { | ||
| init() { #expect(isLoggingConfigured) } | ||
|
|
||
| @Test(arguments: TestDriver.allCases) | ||
| func withConnectionUsesContextLogger(_ driver: TestDriver) async throws { | ||
| try await withDbs(driver) { dbs, _ in | ||
| let (first, second) = try await Self.warmedUpDatabases(dbs) | ||
|
|
||
| try await second.db.withConnection { db in | ||
| try await (db as! any SQLDatabase).raw("SELECT 'inside withConnection'").run() | ||
| } | ||
|
|
||
| #expect(second.logs.loggedQueries.contains("SELECT 'inside withConnection'")) | ||
| #expect(!first.logs.loggedQueries.contains("SELECT 'inside withConnection'")) | ||
| } | ||
| } | ||
|
|
||
| @Test(arguments: TestDriver.allCases) | ||
| func transactionUsesContextLogger(_ driver: TestDriver) async throws { | ||
| try await withDbs(driver) { dbs, _ in | ||
| let (first, second) = try await Self.warmedUpDatabases(dbs) | ||
|
|
||
| try await second.db.transaction { db in | ||
| try await (db as! any SQLDatabase).raw("SELECT 'inside transaction'").run() | ||
| } | ||
|
|
||
| #expect(second.logs.loggedQueries.contains("BEGIN")) | ||
| #expect(second.logs.loggedQueries.contains("SELECT 'inside transaction'")) | ||
| #expect(second.logs.loggedQueries.contains("COMMIT")) | ||
| #expect(!first.logs.loggedQueries.contains("SELECT 'inside transaction'")) | ||
| } | ||
| } | ||
|
|
||
| private static func warmedUpDatabases( | ||
| _ dbs: Databases | ||
| ) async throws -> (first: (db: any Database, logs: InMemoryLogHandler), second: (db: any Database, logs: InMemoryLogHandler)) { | ||
| let eventLoop = dbs.eventLoopGroup.any() | ||
| let firstLogs = InMemoryLogHandler.capturingAllLevels(), secondLogs = InMemoryLogHandler.capturingAllLevels() | ||
| let first = dbs.database(.a, logger: Logger(label: "first") { _ in firstLogs }, on: eventLoop)! | ||
| let second = dbs.database(.a, logger: Logger(label: "second") { _ in secondLogs }, on: eventLoop)! | ||
|
|
||
| try await first.withConnection { db in | ||
| try await (db as! any SQLDatabase).raw("SELECT 'warm up'").run() | ||
| } | ||
| return ((first, firstLogs), (second, secondLogs)) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| extension InMemoryLogHandler { | ||
| /// A handler that captures everything, since SQL queries are logged at `.debug` by default. | ||
| static func capturingAllLevels() -> Self { | ||
| var handler = Self() | ||
| handler.logLevel = .trace | ||
| return handler | ||
| } | ||
|
|
||
| /// The SQL of every query that was logged to this handler. | ||
| var loggedQueries: [String] { | ||
| self.entries.compactMap { entry in | ||
| guard case .string(let sql) = entry.metadata["sql"] else { return nil } | ||
| return sql | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would be nice if we could fix the larger issue with the various versions of
logging(to:)in FluentKit and SQLKit; thanks to protocol dispatch rules, it tends not to work with some APIs. But I think that will require API breakage to deal with.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah once everything is swift concurrency we can just rely on
Logger.current, which is what we do in Vapor 5, resolves so many of these issues