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

fix(logging): make query logging obey level setting for sqlx::query target #945

Merged
merged 1 commit into from
Dec 29, 2020
Merged
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
57 changes: 29 additions & 28 deletions sqlx-core/src/logger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,38 +31,39 @@ impl<'q> QueryLogger<'q> {
self.settings.statements_level
};

if let Some(lvl) = lvl.to_level() {
if lvl <= log::STATIC_MAX_LEVEL && lvl <= log::max_level() {
let mut summary = parse_query_summary(&self.sql);
if let Some(lvl) = lvl
.to_level()
.filter(|lvl| log::log_enabled!(target: "sqlx::query", *lvl))
{
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 = if summary != self.sql {
summary.push_str(" …");
format!(
"\n\n{}\n",
sqlformat::format(
&self.sql,
&sqlformat::QueryParams::None,
sqlformat::FormatOptions::default()
)
} else {
String::new()
};
)
} else {
String::new()
};

let rows = self.rows;
let rows = self.rows;

log::logger().log(
&log::Record::builder()
.args(format_args!(
"{}; rows: {}, elapsed: {:.3?}{}",
summary, rows, elapsed, sql
))
.level(lvl)
.module_path_static(Some("sqlx::query"))
.target("sqlx::query")
.build(),
);
}
log::logger().log(
&log::Record::builder()
.args(format_args!(
"{}; rows: {}, elapsed: {:.3?}{}",
summary, rows, elapsed, sql
))
.level(lvl)
.module_path_static(Some("sqlx::query"))
.target("sqlx::query")
.build(),
);
}
}
}
Expand Down