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

sqlx_logging_level, a wrapper around sqlx::ConnectOptions::log_statements #800

Merged
merged 2 commits into from
Jun 26, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ time = { version = "^0.2", optional = true }
futures = { version = "^0.3" }
futures-util = { version = "^0.3" }
tracing = { version = "0.1", features = ["log"] }
log = "^0.4"
rust_decimal = { version = "^1", optional = true }
sea-orm-macros = { version = "^0.8.0", path = "sea-orm-macros", optional = true }
sea-query = { version = "^0.24.5", features = ["thread-safe"] }
Expand Down
15 changes: 15 additions & 0 deletions src/database/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ pub struct ConnectOptions {
pub(crate) max_lifetime: Option<Duration>,
/// Enable SQLx statement logging
pub(crate) sqlx_logging: bool,
/// SQLx statement logging level (ignored if `sqlx_logging` is false)
pub(crate) sqlx_logging_level: log::LevelFilter,
/// set sqlcipher key
pub(crate) sqlcipher_key: Option<Cow<'static, str>>,
}
Expand Down Expand Up @@ -107,6 +109,7 @@ impl ConnectOptions {
idle_timeout: None,
max_lifetime: None,
sqlx_logging: true,
sqlx_logging_level: log::LevelFilter::Info,
sqlcipher_key: None,
}
}
Expand Down Expand Up @@ -211,6 +214,18 @@ impl ConnectOptions {
self.sqlx_logging
}

/// Set SQLx statement logging level (default INFO)
/// (ignored if `sqlx_logging` is `false`)
pub fn sqlx_logging_level(&mut self, level: log::LevelFilter) -> &mut Self {
self.sqlx_logging_level = level;
self
}

/// Get the level of SQLx statement logging
pub fn get_sqlx_logging_level(&self) -> log::LevelFilter {
self.sqlx_logging_level
}

/// set key for sqlcipher
pub fn sqlcipher_key<T>(&mut self, value: T) -> &mut Self
where
Expand Down
4 changes: 3 additions & 1 deletion src/driver/sqlx_mysql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,11 @@ impl SqlxMySqlConnector {
.url
.parse::<MySqlConnectOptions>()
.map_err(|e| DbErr::Conn(e.to_string()))?;
use sqlx::ConnectOptions;
if !options.sqlx_logging {
use sqlx::ConnectOptions;
opt.disable_statement_logging();
} else {
opt.log_statements(options.sqlx_logging_level);
}
match options.pool_options().connect_with(opt).await {
Ok(pool) => Ok(DatabaseConnection::SqlxMySqlPoolConnection(
Expand Down
4 changes: 3 additions & 1 deletion src/driver/sqlx_postgres.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,11 @@ impl SqlxPostgresConnector {
.url
.parse::<PgConnectOptions>()
.map_err(|e| DbErr::Conn(e.to_string()))?;
use sqlx::ConnectOptions;
if !options.sqlx_logging {
use sqlx::ConnectOptions;
opt.disable_statement_logging();
} else {
opt.log_statements(options.sqlx_logging_level);
}
match options.pool_options().connect_with(opt).await {
Ok(pool) => Ok(DatabaseConnection::SqlxPostgresPoolConnection(
Expand Down
4 changes: 3 additions & 1 deletion src/driver/sqlx_sqlite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,11 @@ impl SqlxSqliteConnector {
if options.sqlcipher_key.is_some() {
opt = opt.pragma("key", options.sqlcipher_key.clone().unwrap());
}
use sqlx::ConnectOptions;
if !options.sqlx_logging {
use sqlx::ConnectOptions;
opt.disable_statement_logging();
} else {
opt.log_statements(options.sqlx_logging_level);
}
if options.get_max_connections().is_none() {
options.max_connections(1);
Expand Down