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:
Anshul Sanghi 2024-01-12 16:46:24 +05:30 committed by GitHub
parent 358e31a09b
commit f019d6ab22
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 47 additions and 0 deletions

View File

@ -54,6 +54,10 @@ pub struct ConnectOptions {
pub(crate) sqlx_logging: bool,
/// SQLx statement logging level (ignored if `sqlx_logging` is false)
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
pub(crate) sqlcipher_key: Option<Cow<'static, str>>,
/// Schema search path (PostgreSQL only)
@ -147,6 +151,8 @@ impl ConnectOptions {
max_lifetime: None,
sqlx_logging: true,
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,
schema_search_path: None,
}
@ -269,11 +275,31 @@ impl ConnectOptions {
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
pub fn get_sqlx_logging_level(&self) -> log::LevelFilter {
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
pub fn sqlcipher_key<T>(&mut self, value: T) -> &mut Self
where

View File

@ -1,3 +1,4 @@
use log::LevelFilter;
use sea_query::Values;
use std::{future::Future, pin::Pin, sync::Arc};
@ -52,6 +53,12 @@ impl SqlxMySqlConnector {
opt = opt.disable_statement_logging();
} else {
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 {
Ok(pool) => Ok(DatabaseConnection::SqlxMySqlPoolConnection(

View File

@ -1,3 +1,4 @@
use log::LevelFilter;
use sea_query::Values;
use std::{future::Future, pin::Pin, sync::Arc};
@ -52,6 +53,12 @@ impl SqlxPostgresConnector {
opt = opt.disable_statement_logging();
} else {
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
.schema_search_path

View File

@ -1,3 +1,4 @@
use log::LevelFilter;
use sea_query::Values;
use std::{future::Future, pin::Pin, sync::Arc};
@ -56,6 +57,12 @@ impl SqlxSqliteConnector {
opt = opt.disable_statement_logging();
} else {
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() {
options.max_connections(1);