Skip to content

Commit

Permalink
sqlx_logging_level, a wrapper around `sqlx::ConnectOptions::log_sta…
Browse files Browse the repository at this point in the history
…tements` (#800)

* sqlx_logging_level

* ` = { version = "..." }`
  • Loading branch information
SandaruKasa authored Jun 26, 2022
1 parent 12ec002 commit 4f26b4a
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 3 deletions.
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 = { version = "^0"}
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

0 comments on commit 4f26b4a

Please sign in to comment.