Debian Codegraph @

src/logger.rs

raw symbols SCIP
1 use craterust-analyzer cargo sqlx-core 0.8.3 crate/::connectionrust-analyzer cargo sqlx-core 0.8.3 connection/::LogSettingsrust-analyzer cargo sqlx-core 0.8.3 connection/LogSettings#;
2 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::timerust-analyzer cargo std https://github.com/rust-lang/rust/library/std time/::Instantrust-analyzer cargo std https://github.com/rust-lang/rust/library/std time/Instant#;
3
4 // Yes these look silly. `tracing` doesn't currently support dynamic levels
5 // https://github.com/tokio-rs/tracing/issues/372
6 #[doc(hidden)]
7 #[macro_export]
8 macro_rules! private_tracing_dynamic_enabledrust-analyzer cargo sqlx-core 0.8.3 logger/private_tracing_dynamic_enabled! {
9 (target: $target:expr, $level:expr) => {{
10 use ::tracing::Level;
11
12 match $level {
13 Level::ERROR => ::tracing::enabled!(target: $target, Level::ERROR),
14 Level::WARN => ::tracing::enabled!(target: $target, Level::WARN),
15 Level::INFO => ::tracing::enabled!(target: $target, Level::INFO),
16 Level::DEBUG => ::tracing::enabled!(target: $target, Level::DEBUG),
17 Level::TRACE => ::tracing::enabled!(target: $target, Level::TRACE),
18 }
19 }};
20 ($level:expr) => {{
21 $crate::private_tracing_dynamic_enabled!(target: module_path!(), $level)
22 }};
23 }
24
25 #[doc(hidden)]
26 #[macro_export]
27 macro_rules! private_tracing_dynamic_eventrust-analyzer cargo sqlx-core 0.8.3 logger/private_tracing_dynamic_event! {
28 (target: $target:expr, $level:expr, $($args:tt)*) => {{
29 use ::tracing::Level;
30
31 match $level {
32 Level::ERROR => ::tracing::event!(target: $target, Level::ERROR, $($args)*),
33 Level::WARN => ::tracing::event!(target: $target, Level::WARN, $($args)*),
34 Level::INFO => ::tracing::event!(target: $target, Level::INFO, $($args)*),
35 Level::DEBUG => ::tracing::event!(target: $target, Level::DEBUG, $($args)*),
36 Level::TRACE => ::tracing::event!(target: $target, Level::TRACE, $($args)*),
37 }
38 }};
39 }
40
41 #[doc(hidden)]
42 pub fn private_level_filter_to_levelsrust-analyzer cargo sqlx-core 0.8.3 logger/private_level_filter_to_levels().(
43 filter: logrust-analyzer cargo log 0.4.34 crate/::LevelFilterrust-analyzer cargo log 0.4.34 LevelFilter#,
44 ) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<(tracingrust-analyzer cargo tracing 0.1.44 crate/::Levelrust-analyzer cargo tracing-core 0.1.36 metadata/Level#, logrust-analyzer cargo log 0.4.34 crate/::Levelrust-analyzer cargo log 0.4.34 Level#)> {
45 let tracing_level = match filter {
46 logrust-analyzer cargo log 0.4.34 crate/::LevelFilterrust-analyzer cargo log 0.4.34 LevelFilter#::Errorrust-analyzer cargo log 0.4.34 LevelFilter#Error# => Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(tracingrust-analyzer cargo tracing 0.1.44 crate/::Levelrust-analyzer cargo tracing-core 0.1.36 metadata/Level#::ERRORrust-analyzer cargo tracing-core 0.1.36 metadata/impl#[Level]ERROR.),
47 logrust-analyzer cargo log 0.4.34 crate/::LevelFilterrust-analyzer cargo log 0.4.34 LevelFilter#::Warnrust-analyzer cargo log 0.4.34 LevelFilter#Warn# => Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(tracingrust-analyzer cargo tracing 0.1.44 crate/::Levelrust-analyzer cargo tracing-core 0.1.36 metadata/Level#::WARNrust-analyzer cargo tracing-core 0.1.36 metadata/impl#[Level]WARN.),
48 logrust-analyzer cargo log 0.4.34 crate/::LevelFilterrust-analyzer cargo log 0.4.34 LevelFilter#::Inforust-analyzer cargo log 0.4.34 LevelFilter#Info# => Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(tracingrust-analyzer cargo tracing 0.1.44 crate/::Levelrust-analyzer cargo tracing-core 0.1.36 metadata/Level#::INFOrust-analyzer cargo tracing-core 0.1.36 metadata/impl#[Level]INFO.),
49 logrust-analyzer cargo log 0.4.34 crate/::LevelFilterrust-analyzer cargo log 0.4.34 LevelFilter#::Debugrust-analyzer cargo log 0.4.34 LevelFilter#Debug# => Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(tracingrust-analyzer cargo tracing 0.1.44 crate/::Levelrust-analyzer cargo tracing-core 0.1.36 metadata/Level#::DEBUGrust-analyzer cargo tracing-core 0.1.36 metadata/impl#[Level]DEBUG.),
50 logrust-analyzer cargo log 0.4.34 crate/::LevelFilterrust-analyzer cargo log 0.4.34 LevelFilter#::Tracerust-analyzer cargo log 0.4.34 LevelFilter#Trace# => Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(tracingrust-analyzer cargo tracing 0.1.44 crate/::Levelrust-analyzer cargo tracing-core 0.1.36 metadata/Level#::TRACErust-analyzer cargo tracing-core 0.1.36 metadata/impl#[Level]TRACE.),
51 logrust-analyzer cargo log 0.4.34 crate/::LevelFilterrust-analyzer cargo log 0.4.34 LevelFilter#::Offrust-analyzer cargo log 0.4.34 LevelFilter#Off# => Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#,
52 };
53
54 tracing_level.ziprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]zip().(filter.to_levelrust-analyzer cargo log 0.4.34 impl#[LevelFilter]to_level().())
55 }
56
57 pub(craterust-analyzer cargo sqlx-core 0.8.3 crate/) fn private_level_filter_to_trace_levelrust-analyzer cargo sqlx-core 0.8.3 logger/private_level_filter_to_trace_level().(
58 filter: logrust-analyzer cargo log 0.4.34 crate/::LevelFilterrust-analyzer cargo log 0.4.34 LevelFilter#,
59 ) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<tracingrust-analyzer cargo tracing 0.1.44 crate/::Levelrust-analyzer cargo tracing-core 0.1.36 metadata/Level#> {
60 private_level_filter_to_levelsrust-analyzer cargo sqlx-core 0.8.3 logger/private_level_filter_to_levels().(filter).maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]map().(|(level, _)| level)
61 }
62
63 pub struct QueryLoggerrust-analyzer cargo sqlx-core 0.8.3 logger/QueryLogger#<'q> {
64 sqlrust-analyzer cargo sqlx-core 0.8.3 logger/QueryLogger#sql.: &'q str,
65 rows_returnedrust-analyzer cargo sqlx-core 0.8.3 logger/QueryLogger#rows_returned.: u64,
66 rows_affectedrust-analyzer cargo sqlx-core 0.8.3 logger/QueryLogger#rows_affected.: u64,
67 startrust-analyzer cargo sqlx-core 0.8.3 logger/QueryLogger#start.: Instantrust-analyzer cargo std https://github.com/rust-lang/rust/library/std time/Instant#,
68 settingsrust-analyzer cargo sqlx-core 0.8.3 logger/QueryLogger#settings.: LogSettingsrust-analyzer cargo sqlx-core 0.8.3 connection/LogSettings#,
69 }
70
71 impl<'q> QueryLoggerrust-analyzer cargo sqlx-core 0.8.3 logger/QueryLogger#<'q> {
72 pub fn newrust-analyzer cargo sqlx-core 0.8.3 logger/impl#[`QueryLogger<'q>`]new().(sql: &'q str, settings: LogSettingsrust-analyzer cargo sqlx-core 0.8.3 connection/LogSettings#) -> Selfrust-analyzer cargo sqlx-core 0.8.3 logger/impl#[`QueryLogger<'q>`] {
73 Selfrust-analyzer cargo sqlx-core 0.8.3 logger/impl#[`QueryLogger<'q>`] {
74 sqlrust-analyzer cargo sqlx-core 0.8.3 logger/QueryLogger#sql.,
75 rows_returnedrust-analyzer cargo sqlx-core 0.8.3 logger/QueryLogger#rows_returned.: 0,
76 rows_affectedrust-analyzer cargo sqlx-core 0.8.3 logger/QueryLogger#rows_affected.: 0,
77 startrust-analyzer cargo sqlx-core 0.8.3 logger/QueryLogger#start.: Instantrust-analyzer cargo std https://github.com/rust-lang/rust/library/std time/Instant#::nowrust-analyzer cargo std https://github.com/rust-lang/rust/library/std time/impl#[Instant]now().(),
78 settingsrust-analyzer cargo sqlx-core 0.8.3 logger/QueryLogger#settings.,
79 }
80 }
81
82 pub fn increment_rows_returnedrust-analyzer cargo sqlx-core 0.8.3 logger/impl#[`QueryLogger<'q>`]increment_rows_returned().(&mut self) {
83 self.rows_returnedrust-analyzer cargo sqlx-core 0.8.3 logger/QueryLogger#rows_returned. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u64][`AddAssign<Self>`]add_assign().+=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u64][`AddAssign<Self>`]add_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u64][`AddAssign<Self>`]add_assign().1;
84 }
85
86 pub fn increase_rows_affectedrust-analyzer cargo sqlx-core 0.8.3 logger/impl#[`QueryLogger<'q>`]increase_rows_affected().(&mut self, n: u64) {
87 self.rows_affectedrust-analyzer cargo sqlx-core 0.8.3 logger/QueryLogger#rows_affected. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u64][`AddAssign<Self>`]add_assign().+=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u64][`AddAssign<Self>`]add_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u64][`AddAssign<Self>`]add_assign().n;
88 }
89
90 pub fn finishrust-analyzer cargo sqlx-core 0.8.3 logger/impl#[`QueryLogger<'q>`]finish().(&self) {
91 let elapsed = self.startrust-analyzer cargo sqlx-core 0.8.3 logger/QueryLogger#start..elapsedrust-analyzer cargo std https://github.com/rust-lang/rust/library/std time/impl#[Instant]elapsed().();
92
93 let was_slow = elapsed rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialOrd#ge().>=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialOrd#ge(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialOrd#ge().self.settingsrust-analyzer cargo sqlx-core 0.8.3 logger/QueryLogger#settings..slow_statements_durationrust-analyzer cargo sqlx-core 0.8.3 connection/LogSettings#slow_statements_duration.;
94
95 let lvl = if was_slow {
96 self.settingsrust-analyzer cargo sqlx-core 0.8.3 logger/QueryLogger#settings..slow_statements_levelrust-analyzer cargo sqlx-core 0.8.3 connection/LogSettings#slow_statements_level.
97 } else {
98 self.settingsrust-analyzer cargo sqlx-core 0.8.3 logger/QueryLogger#settings..statements_levelrust-analyzer cargo sqlx-core 0.8.3 connection/LogSettings#statements_level.
99 };
100
101 if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#((tracing_level, log_level)) = private_level_filter_to_levelsrust-analyzer cargo sqlx-core 0.8.3 logger/private_level_filter_to_levels().(lvl) {
102 // The enabled level could be set from either tracing world or log world, so check both
103 // to see if logging should be enabled for our level
104 let log_is_enabled = logrust-analyzer cargo log 0.4.34 crate/::log_enabledrust-analyzer cargo log 0.4.34 macros/log_enabled!!(target: "sqlx::query", log_level)
105 || private_tracing_dynamic_enabledrust-analyzer cargo sqlx-core 0.8.3 logger/private_tracing_dynamic_enabled!!(target: "sqlx::query", tracing_level);
106 if log_is_enabled {
107 let mut summary = parse_query_summaryrust-analyzer cargo sqlx-core 0.8.3 logger/parse_query_summary().(self.sqlrust-analyzer cargo sqlx-core 0.8.3 logger/QueryLogger#sql.);
108
109 let sql = if summary rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String][`PartialEq<&str>`]ne().!=rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String][`PartialEq<&str>`]ne(). rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String][`PartialEq<&str>`]ne().self.sqlrust-analyzer cargo sqlx-core 0.8.3 logger/QueryLogger#sql. {
110 summary.push_strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push_str().(" …");
111 formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!(
112 "\n\n{}\n",
113 self.sqlrust-analyzer cargo sqlx-core 0.8.3 logger/QueryLogger#sql. /*
114 sqlformat::format(
115 self.sql,
116 &sqlformat::QueryParams::None,
117 sqlformat::FormatOptions::default()
118 )*/
119 )
120 } else {
121 Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]new().()
122 };
123
124 if was_slow {
125 private_tracing_dynamic_eventrust-analyzer cargo sqlx-core 0.8.3 logger/private_tracing_dynamic_event!!(
126 target: "sqlx::query",
127 tracing_level,
128 summary,
129 db.statement = sql,
130 rows_affected = self.rows_affectedrust-analyzer cargo sqlx-core 0.8.3 logger/QueryLogger#rows_affected.,
131 rows_returned = self.rows_returnedrust-analyzer cargo sqlx-core 0.8.3 logger/QueryLogger#rows_returned.,
132 // Human-friendly - includes units (usually ms). Also kept for backward compatibility
133 ?elapsed,
134 // Search friendly - numeric
135 elapsed_secs = elapsed.as_secs_f64rust-analyzer cargo core https://github.com/rust-lang/rust/library/core time/impl#[Duration]as_secs_f64().(),
136 // When logging to JSON, one can trigger alerts from the presence of this field.
137 slow_threshold=?self.settingsrust-analyzer cargo sqlx-core 0.8.3 logger/QueryLogger#settings..slow_statements_durationrust-analyzer cargo sqlx-core 0.8.3 connection/LogSettings#slow_statements_duration.,
138 // Make sure to use "slow" in the message as that's likely
139 // what people will grep for.
140 "slow statement: execution time exceeded alert threshold"
141 );
142 } else {
143 private_tracing_dynamic_eventrust-analyzer cargo sqlx-core 0.8.3 logger/private_tracing_dynamic_event!!(
144 target: "sqlx::query",
145 tracing_level,
146 summary,
147 db.statement = sql,
148 rows_affected = self.rows_affectedrust-analyzer cargo sqlx-core 0.8.3 logger/QueryLogger#rows_affected.,
149 rows_returned = self.rows_returnedrust-analyzer cargo sqlx-core 0.8.3 logger/QueryLogger#rows_returned.,
150 // Human-friendly - includes units (usually ms). Also kept for backward compatibility
151 ?elapsed,
152 // Search friendly - numeric
153 elapsed_secs = elapsed.as_secs_f64rust-analyzer cargo core https://github.com/rust-lang/rust/library/core time/impl#[Duration]as_secs_f64().(),
154 );
155 }
156 }
157 }
158 }
159 }
160
161 impl<'q> Droprust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/drop/Drop# for QueryLoggerrust-analyzer cargo sqlx-core 0.8.3 logger/QueryLogger#<'q> {
162 fn droprust-analyzer cargo sqlx-core 0.8.3 logger/impl#[`QueryLogger<'q>`][Drop]drop().(&mut self) {
163 self.finishrust-analyzer cargo sqlx-core 0.8.3 logger/impl#[`QueryLogger<'q>`]finish().();
164 }
165 }
166
167 pub fn parse_query_summaryrust-analyzer cargo sqlx-core 0.8.3 logger/parse_query_summary().(sql: &str) -> Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String# {
168 // For now, just take the first 4 words
169 sql.split_whitespacerust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]split_whitespace().()
170 .takerust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#take().(4)
171 .collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().::<Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<&str>>()
172 .joinrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc slice/impl#[`[T]`]join().(" ")
173 }