Skip to content
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

refactor(logging): change slow query log message #2795

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 8 additions & 23 deletions sqlx-core/src/logger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,26 +98,19 @@ impl<'q> QueryLogger<'q> {
let log_is_enabled = log::log_enabled!(target: "sqlx::query", log_level)
|| private_tracing_dynamic_enabled!(target: "sqlx::query", tracing_level);
if log_is_enabled {
let mut summary = parse_query_summary(&self.sql);

let sql = if summary != self.sql {
summary.push_str(" …");
format!(
"\n\n{}\n",
sqlformat::format(
&self.sql,
&sqlformat::QueryParams::None,
sqlformat::FormatOptions::default()
)
let sql = format!(
"{}\n",
sqlformat::format(
&self.sql,
&sqlformat::QueryParams::None,
sqlformat::FormatOptions::default()
)
} else {
String::new()
};
);

private_tracing_dynamic_event!(
target: "sqlx::query",
tracing_level,
summary,
summary = format!("query went over configured duration of {}ms", self.settings.slow_statements_duration.as_millis()),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As written, the message will be the same even if the query didn't exceed the threshold.

We should also not be changing the contents of these fields.

db.statement = sql,
rows_affected = self.rows_affected,
rows_returned= self.rows_returned,
Expand All @@ -133,11 +126,3 @@ impl<'q> Drop for QueryLogger<'q> {
self.finish();
}
}

pub fn parse_query_summary(sql: &str) -> String {
// For now, just take the first 4 words
sql.split_whitespace()
.take(4)
.collect::<Vec<&str>>()
.join(" ")
}
8 changes: 8 additions & 0 deletions sqlx-sqlite/src/logger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,11 @@ impl<'q, O: Debug + Hash + Eq, R: Debug, P: Debug> Drop for QueryPlanLogger<'q,
self.finish();
}
}

fn parse_query_summary(sql: &str) -> String {
// For now, just take the first 4 words
sql.split_whitespace()
.take(4)
.collect::<Vec<&str>>()
.join(" ")
}
Loading