Skip to content

Commit

Permalink
feat: disable sqlx logging for all statements
Browse files Browse the repository at this point in the history
When the loggin level is set to INFO sqlx logs all SQL statements like
this:

```
2023-06-06T17:19:52.639651375+01:00 [sqlx::query][INFO] SELECT version FROM _sqlx_migrations …; rows affected: 0, rows returned: 0, elapsed: 58.361µs

SELECT
  version
FROM
  _sqlx_migrations
WHERE
  success = false
ORDER BY
  version
LIMIT
  1
```

This commits makes sqlc to log only failed statements as errors and slow
statements (>1 second) as warnings.
  • Loading branch information
josecelano committed Jun 6, 2023
1 parent 7347fee commit 7bcf20e
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/bootstrap/logging.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ pub fn setup() {

fn config_level_or_default(log_level: &Option<String>) -> LevelFilter {
match log_level {
None => log::LevelFilter::Warn,
None => log::LevelFilter::Info,
Some(level) => LevelFilter::from_str(level).unwrap(),
}
}
Expand Down
14 changes: 11 additions & 3 deletions src/databases/mysql.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
use std::str::FromStr;
use std::time::Duration;

use async_trait::async_trait;
use chrono::NaiveDateTime;
use sqlx::mysql::MySqlPoolOptions;
use sqlx::{query, query_as, Acquire, MySqlPool};
use sqlx::mysql::{MySqlConnectOptions, MySqlPoolOptions};
use sqlx::{query, query_as, Acquire, ConnectOptions, MySqlPool};

use crate::databases::database;
use crate::databases::database::{Category, Database, Driver, Sorting, TorrentCompact};
Expand All @@ -25,8 +28,13 @@ impl Database for Mysql {
}

async fn new(database_url: &str) -> Self {
let mut connection_options = MySqlConnectOptions::from_str(database_url).expect("Unable to create connection options.");
connection_options
.log_statements(log::LevelFilter::Error)
.log_slow_statements(log::LevelFilter::Warn, Duration::from_secs(1));

let db = MySqlPoolOptions::new()
.connect(database_url)
.connect_with(connection_options)
.await
.expect("Unable to create database pool.");

Expand Down
14 changes: 11 additions & 3 deletions src/databases/sqlite.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
use std::str::FromStr;
use std::time::Duration;

use async_trait::async_trait;
use chrono::NaiveDateTime;
use sqlx::sqlite::SqlitePoolOptions;
use sqlx::{query, query_as, Acquire, SqlitePool};
use sqlx::sqlite::{SqliteConnectOptions, SqlitePoolOptions};
use sqlx::{query, query_as, Acquire, ConnectOptions, SqlitePool};

use crate::databases::database;
use crate::databases::database::{Category, Database, Driver, Sorting, TorrentCompact};
Expand All @@ -25,8 +28,13 @@ impl Database for Sqlite {
}

async fn new(database_url: &str) -> Self {
let mut connection_options = SqliteConnectOptions::from_str(database_url).expect("Unable to create connection options.");
connection_options
.log_statements(log::LevelFilter::Error)
.log_slow_statements(log::LevelFilter::Warn, Duration::from_secs(1));

let db = SqlitePoolOptions::new()
.connect(database_url)
.connect_with(connection_options)
.await
.expect("Unable to create database pool.");

Expand Down

0 comments on commit 7bcf20e

Please sign in to comment.