Skip to content

Commit

Permalink
require trace log level instead of using whatever log level statement…
Browse files Browse the repository at this point in the history
… logging was configured to use
  • Loading branch information
tyrelr committed Mar 11, 2024
1 parent b3dce62 commit 526ab7b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 41 deletions.
3 changes: 1 addition & 2 deletions sqlx-sqlite/src/connection/explain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -566,8 +566,7 @@ pub(super) fn explain(
.collect::<Result<Vec<_>, Error>>()?;
let program_size = program.len();

let mut logger =
crate::logger::QueryPlanLogger::new(query, &program, conn.log_settings.clone());
let mut logger = crate::logger::QueryPlanLogger::new(query, &program);
let mut branch_seq = Sequence::new();
let mut states = BranchList::new(QueryState {
visited: vec![0; program_size],
Expand Down
64 changes: 25 additions & 39 deletions sqlx-sqlite/src/logger.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use crate::connection::intmap::IntMap;
use sqlx_core::{connection::LogSettings, logger};
use std::collections::HashSet;
use std::fmt::Debug;
use std::hash::Hash;
Expand Down Expand Up @@ -40,7 +39,6 @@ pub struct QueryPlanLogger<'q, R: Debug + 'static, S: Debug + DebugDiff + 'stati
branch_results: IntMap<BranchResult<R>>,
branch_operations: IntMap<IntMap<InstructionHistory<S>>>,
program: &'q [P],
settings: LogSettings,
}

/// convert a string into dot format
Expand Down Expand Up @@ -338,27 +336,20 @@ impl<R: Debug, S: Debug + DebugDiff, P: Debug> core::fmt::Display for QueryPlanL
}

impl<'q, R: Debug, S: Debug + DebugDiff, P: Debug> QueryPlanLogger<'q, R, S, P> {
pub fn new(sql: &'q str, program: &'q [P], settings: LogSettings) -> Self {
pub fn new(sql: &'q str, program: &'q [P]) -> Self {
Self {
sql,
unknown_operations: HashSet::new(),
branch_origins: IntMap::new(),
branch_results: IntMap::new(),
branch_operations: IntMap::new(),
program,
settings,
}
}

pub fn log_enabled(&self) -> bool {
if let Some((tracing_level, log_level)) =
logger::private_level_filter_to_levels(self.settings.statements_level)
{
log::log_enabled!(target: "sqlx::explain", log_level)
|| private_tracing_dynamic_enabled!(target: "sqlx::explain", tracing_level)
} else {
false
}
log::log_enabled!(target: "sqlx::explain", log::Level::Trace)
|| private_tracing_dynamic_enabled!(target: "sqlx::explain", tracing::Level::TRACE)
}

pub fn add_branch<I: Copy>(&mut self, state: I, parent: &BranchParent)
Expand Down Expand Up @@ -410,33 +401,28 @@ impl<'q, R: Debug, S: Debug + DebugDiff, P: Debug> QueryPlanLogger<'q, R, S, P>
if !self.log_enabled() {
return;
}
let lvl = self.settings.statements_level;

if let Some((tracing_level, _)) = logger::private_level_filter_to_levels(lvl) {
if self.log_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()
)
)
} else {
String::new()
};

sqlx_core::private_tracing_dynamic_event!(
target: "sqlx::explain",
tracing_level,
"{}; program:\n{}\n\n{:?}", summary, self, sql
);
}
}

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()
)
)
} else {
String::new()
};

sqlx_core::private_tracing_dynamic_event!(
target: "sqlx::explain",
tracing::Level::TRACE,
"{}; program:\n{}\n\n{:?}", summary, self, sql
);
}
}

Expand Down

0 comments on commit 526ab7b

Please sign in to comment.