Add Ability To Configure SQLx Slow Statement Logging Settings (#2055)
* Add Ability To Configure SQLx Slow Statement Logging Settings * Disable SQLX Slow Statement Logging By Default
This commit is contained in:
parent
358e31a09b
commit
f019d6ab22
@ -54,6 +54,10 @@ pub struct ConnectOptions {
|
|||||||
pub(crate) sqlx_logging: bool,
|
pub(crate) sqlx_logging: bool,
|
||||||
/// SQLx statement logging level (ignored if `sqlx_logging` is false)
|
/// SQLx statement logging level (ignored if `sqlx_logging` is false)
|
||||||
pub(crate) sqlx_logging_level: log::LevelFilter,
|
pub(crate) sqlx_logging_level: log::LevelFilter,
|
||||||
|
/// SQLx slow statements logging level (ignored if `sqlx_logging` is false)
|
||||||
|
pub(crate) sqlx_slow_statements_logging_level: log::LevelFilter,
|
||||||
|
/// SQLx slow statements duration threshold (ignored if `sqlx_logging` is false)
|
||||||
|
pub(crate) sqlx_slow_statements_logging_threshold: Duration,
|
||||||
/// set sqlcipher key
|
/// set sqlcipher key
|
||||||
pub(crate) sqlcipher_key: Option<Cow<'static, str>>,
|
pub(crate) sqlcipher_key: Option<Cow<'static, str>>,
|
||||||
/// Schema search path (PostgreSQL only)
|
/// Schema search path (PostgreSQL only)
|
||||||
@ -147,6 +151,8 @@ impl ConnectOptions {
|
|||||||
max_lifetime: None,
|
max_lifetime: None,
|
||||||
sqlx_logging: true,
|
sqlx_logging: true,
|
||||||
sqlx_logging_level: log::LevelFilter::Info,
|
sqlx_logging_level: log::LevelFilter::Info,
|
||||||
|
sqlx_slow_statements_logging_level: log::LevelFilter::Off,
|
||||||
|
sqlx_slow_statements_logging_threshold: Duration::from_secs(1),
|
||||||
sqlcipher_key: None,
|
sqlcipher_key: None,
|
||||||
schema_search_path: None,
|
schema_search_path: None,
|
||||||
}
|
}
|
||||||
@ -269,11 +275,31 @@ impl ConnectOptions {
|
|||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Set SQLx slow statements logging level and duration threshold
|
||||||
|
/// (ignored if `sqlx_logging` is `false`)
|
||||||
|
pub fn sqlx_slow_statements_logging_settings(
|
||||||
|
&mut self,
|
||||||
|
level: log::LevelFilter,
|
||||||
|
duration: Duration,
|
||||||
|
) -> &mut Self {
|
||||||
|
self.sqlx_slow_statements_logging_level = level;
|
||||||
|
self.sqlx_slow_statements_logging_threshold = duration;
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
/// Get the level of SQLx statement logging
|
/// Get the level of SQLx statement logging
|
||||||
pub fn get_sqlx_logging_level(&self) -> log::LevelFilter {
|
pub fn get_sqlx_logging_level(&self) -> log::LevelFilter {
|
||||||
self.sqlx_logging_level
|
self.sqlx_logging_level
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Get the SQLx slow statements logging settings
|
||||||
|
pub fn get_sqlx_slow_statements_logging_settings(&self) -> (log::LevelFilter, Duration) {
|
||||||
|
(
|
||||||
|
self.sqlx_slow_statements_logging_level,
|
||||||
|
self.sqlx_slow_statements_logging_threshold,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
/// set key for sqlcipher
|
/// set key for sqlcipher
|
||||||
pub fn sqlcipher_key<T>(&mut self, value: T) -> &mut Self
|
pub fn sqlcipher_key<T>(&mut self, value: T) -> &mut Self
|
||||||
where
|
where
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
use log::LevelFilter;
|
||||||
use sea_query::Values;
|
use sea_query::Values;
|
||||||
use std::{future::Future, pin::Pin, sync::Arc};
|
use std::{future::Future, pin::Pin, sync::Arc};
|
||||||
|
|
||||||
@ -52,6 +53,12 @@ impl SqlxMySqlConnector {
|
|||||||
opt = opt.disable_statement_logging();
|
opt = opt.disable_statement_logging();
|
||||||
} else {
|
} else {
|
||||||
opt = opt.log_statements(options.sqlx_logging_level);
|
opt = opt.log_statements(options.sqlx_logging_level);
|
||||||
|
if options.sqlx_slow_statements_logging_level != LevelFilter::Off {
|
||||||
|
opt = opt.log_slow_statements(
|
||||||
|
options.sqlx_slow_statements_logging_level,
|
||||||
|
options.sqlx_slow_statements_logging_threshold,
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
match options.pool_options().connect_with(opt).await {
|
match options.pool_options().connect_with(opt).await {
|
||||||
Ok(pool) => Ok(DatabaseConnection::SqlxMySqlPoolConnection(
|
Ok(pool) => Ok(DatabaseConnection::SqlxMySqlPoolConnection(
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
use log::LevelFilter;
|
||||||
use sea_query::Values;
|
use sea_query::Values;
|
||||||
use std::{future::Future, pin::Pin, sync::Arc};
|
use std::{future::Future, pin::Pin, sync::Arc};
|
||||||
|
|
||||||
@ -52,6 +53,12 @@ impl SqlxPostgresConnector {
|
|||||||
opt = opt.disable_statement_logging();
|
opt = opt.disable_statement_logging();
|
||||||
} else {
|
} else {
|
||||||
opt = opt.log_statements(options.sqlx_logging_level);
|
opt = opt.log_statements(options.sqlx_logging_level);
|
||||||
|
if options.sqlx_slow_statements_logging_level != LevelFilter::Off {
|
||||||
|
opt = opt.log_slow_statements(
|
||||||
|
options.sqlx_slow_statements_logging_level,
|
||||||
|
options.sqlx_slow_statements_logging_threshold,
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
let set_search_path_sql = options
|
let set_search_path_sql = options
|
||||||
.schema_search_path
|
.schema_search_path
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
use log::LevelFilter;
|
||||||
use sea_query::Values;
|
use sea_query::Values;
|
||||||
use std::{future::Future, pin::Pin, sync::Arc};
|
use std::{future::Future, pin::Pin, sync::Arc};
|
||||||
|
|
||||||
@ -56,6 +57,12 @@ impl SqlxSqliteConnector {
|
|||||||
opt = opt.disable_statement_logging();
|
opt = opt.disable_statement_logging();
|
||||||
} else {
|
} else {
|
||||||
opt = opt.log_statements(options.sqlx_logging_level);
|
opt = opt.log_statements(options.sqlx_logging_level);
|
||||||
|
if options.sqlx_slow_statements_logging_level != LevelFilter::Off {
|
||||||
|
opt = opt.log_slow_statements(
|
||||||
|
options.sqlx_slow_statements_logging_level,
|
||||||
|
options.sqlx_slow_statements_logging_threshold,
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if options.get_max_connections().is_none() {
|
if options.get_max_connections().is_none() {
|
||||||
options.max_connections(1);
|
options.max_connections(1);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user