Skip to content

Commit 719d800

Browse files
authored
feat: allow log level customization (launchbadge#1371)
1 parent 335eed4 commit 719d800

File tree

4 files changed

+59
-8
lines changed

4 files changed

+59
-8
lines changed

sqlx-core/src/connection.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::database::{Database, HasStatementCache};
22
use crate::error::Error;
33
use crate::transaction::Transaction;
44
use futures_core::future::BoxFuture;
5-
use log::LevelFilter;
5+
pub use log::LevelFilter;
66
use std::fmt::Debug;
77
use std::str::FromStr;
88
use std::time::Duration;
@@ -126,27 +126,27 @@ pub trait Connection: Send {
126126
}
127127

128128
#[derive(Clone, Debug)]
129-
pub(crate) struct LogSettings {
130-
pub(crate) statements_level: LevelFilter,
131-
pub(crate) slow_statements_level: LevelFilter,
132-
pub(crate) slow_statements_duration: Duration,
129+
pub struct LogSettings {
130+
pub statements_level: LevelFilter,
131+
pub slow_statements_level: LevelFilter,
132+
pub slow_statements_duration: Duration,
133133
}
134134

135135
impl Default for LogSettings {
136136
fn default() -> Self {
137137
LogSettings {
138-
statements_level: LevelFilter::Info,
138+
statements_level: LevelFilter::Debug,
139139
slow_statements_level: LevelFilter::Warn,
140140
slow_statements_duration: Duration::from_secs(1),
141141
}
142142
}
143143
}
144144

145145
impl LogSettings {
146-
pub(crate) fn log_statements(&mut self, level: LevelFilter) {
146+
pub fn log_statements(&mut self, level: LevelFilter) {
147147
self.statements_level = level;
148148
}
149-
pub(crate) fn log_slow_statements(&mut self, level: LevelFilter, duration: Duration) {
149+
pub fn log_slow_statements(&mut self, level: LevelFilter, duration: Duration) {
150150
self.slow_statements_level = level;
151151
self.slow_statements_duration = duration;
152152
}

sqlx-core/src/mysql/options/mod.rs

+17
Original file line numberDiff line numberDiff line change
@@ -212,4 +212,21 @@ impl MySqlConnectOptions {
212212
self.collation = Some(collation.to_owned());
213213
self
214214
}
215+
216+
/// Sets the log settings.
217+
///
218+
/// # Example
219+
///
220+
/// ```rust
221+
/// # use sqlx_core::mysql::MySqlConnectOptions;
222+
/// let options = MySqlConnectOptions::new()
223+
/// .log_settings(sqlx_core::connection::LogSettings {
224+
/// statements_level: sqlx_core::connection::LevelFilter::Info,
225+
/// ..Default::default()
226+
/// });
227+
/// ```
228+
pub fn log_settings(mut self, log_settings: LogSettings) -> Self {
229+
self.log_settings = log_settings;
230+
self
231+
}
215232
}

sqlx-core/src/postgres/options/mod.rs

+17
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,23 @@ impl PgConnectOptions {
319319
self
320320
}
321321

322+
/// Sets the log settings.
323+
///
324+
/// # Example
325+
///
326+
/// ```rust
327+
/// # use sqlx_core::postgres::PgConnectOptions;
328+
/// let options = PgConnectOptions::new()
329+
/// .log_settings(sqlx_core::connection::LogSettings {
330+
/// statements_level: sqlx_core::connection::LevelFilter::Info,
331+
/// ..Default::default()
332+
/// });
333+
/// ```
334+
pub fn log_settings(mut self, log_settings: LogSettings) -> Self {
335+
self.log_settings = log_settings;
336+
self
337+
}
338+
322339
/// We try using a socket if hostname starts with `/` or if socket parameter
323340
/// is specified.
324341
pub(crate) fn fetch_socket(&self) -> Option<String> {

sqlx-core/src/sqlite/options/mod.rs

+17
Original file line numberDiff line numberDiff line change
@@ -190,4 +190,21 @@ impl SqliteConnectOptions {
190190
self.page_size = page_size;
191191
self
192192
}
193+
194+
/// Sets the log settings.
195+
///
196+
/// # Example
197+
///
198+
/// ```rust
199+
/// # use sqlx_core::sqlite::SqliteConnectOptions;
200+
/// let options = SqliteConnectOptions::new()
201+
/// .log_settings(sqlx_core::connection::LogSettings {
202+
/// statements_level: sqlx_core::connection::LevelFilter::Info,
203+
/// ..Default::default()
204+
/// });
205+
/// ```
206+
pub fn log_settings(mut self, log_settings: LogSettings) -> Self {
207+
self.log_settings = log_settings;
208+
self
209+
}
193210
}

0 commit comments

Comments
 (0)