Skip to content

Commit

Permalink
refactor(logging): change slow query log message
Browse files Browse the repository at this point in the history
Current summary is just a truncated version of the also provided full sql statement.

New version attempts to more clearly inform user of what exactly is causing the warn log.

This could be considered a breaking change, but given that the summary was just a trucated full statement it should be fine.
  • Loading branch information
RoloEdits committed Oct 4, 2023
1 parent e80291b commit 84d3b25
Showing 1 changed file with 8 additions and 23 deletions.
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()),
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(" ")
}

0 comments on commit 84d3b25

Please sign in to comment.