diff --git a/Cargo.lock b/Cargo.lock index 7d3411b..a396c2a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -8,6 +8,15 @@ version = "2.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "320119579fcad9c21884f5c4861d16174d0e06250625266f50fe6898340abefa" +[[package]] +name = "aho-corasick" +version = "1.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ddd31a130427c27518df266943a5308ed92d4b226cc639f5a8f1002816174301" +dependencies = [ + "memchr", +] + [[package]] name = "alloc-no-stdlib" version = "2.0.4" @@ -32,6 +41,56 @@ dependencies = [ "libc", ] +[[package]] +name = "anstream" +version = "0.6.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "43d5b281e737544384e969a5ccad3f1cdd24b48086a0fc1b2a5262a26b8f4f4a" +dependencies = [ + "anstyle", + "anstyle-parse", + "anstyle-query", + "anstyle-wincon", + "colorchoice", + "is_terminal_polyfill", + "utf8parse", +] + +[[package]] +name = "anstyle" +version = "1.0.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5192cca8006f1fd4f7237516f40fa183bb07f8fbdfedaa0036de5ea9b0b45e78" + +[[package]] +name = "anstyle-parse" +version = "0.2.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4e7644824f0aa2c7b9384579234ef10eb7efb6a0deb83f9630a49594dd9c15c2" +dependencies = [ + "utf8parse", +] + +[[package]] +name = "anstyle-query" +version = "1.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "40c48f72fd53cd289104fc64099abca73db4166ad86ea0b4341abe65af83dadc" +dependencies = [ + "windows-sys 0.61.2", +] + +[[package]] +name = "anstyle-wincon" +version = "3.0.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "291e6a250ff86cd4a820112fb8898808a366d8f9f58ce16d1f538353ad55747d" +dependencies = [ + "anstyle", + "once_cell_polyfill", + "windows-sys 0.61.2", +] + [[package]] name = "anyhow" version = "1.0.102" @@ -260,6 +319,12 @@ dependencies = [ "phf", ] +[[package]] +name = "colorchoice" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b05b61dc5112cbb17e4b6cd61790d9845d13888356391624cbe7e41efeac1e75" + [[package]] name = "compression-codecs" version = "0.4.37" @@ -340,6 +405,29 @@ version = "0.15.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1aaf95b3e5c8f23aa320147307562d361db0ae0d51242340f558153b4eb2439b" +[[package]] +name = "env_filter" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7a1c3cc8e57274ec99de65301228b537f1e4eedc1b8e0f9411c6caac8ae7308f" +dependencies = [ + "log", + "regex", +] + +[[package]] +name = "env_logger" +version = "0.11.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b2daee4ea451f429a58296525ddf28b45a3b64f1acf6587e2067437bb11e218d" +dependencies = [ + "anstream", + "anstyle", + "env_filter", + "jiff", + "log", +] + [[package]] name = "equivalent" version = "1.0.2" @@ -673,12 +761,42 @@ dependencies = [ "serde", ] +[[package]] +name = "is_terminal_polyfill" +version = "1.70.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a6cb138bb79a146c1bd460005623e142ef0181e3d0219cb493e02f7d08a35695" + [[package]] name = "itoa" version = "1.0.17" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "92ecc6618181def0457392ccd0ee51198e065e016d1d527a7ac1b6dc7c1f09d2" +[[package]] +name = "jiff" +version = "0.2.22" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "819b44bc7c87d9117eb522f14d46e918add69ff12713c475946b0a29363ed1c2" +dependencies = [ + "jiff-static", + "log", + "portable-atomic", + "portable-atomic-util", + "serde_core", +] + +[[package]] +name = "jiff-static" +version = "0.2.22" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "470252db18ecc35fd766c0891b1e3ec6cbbcd62507e85276c01bf75d8e94d4a1" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "jobserver" version = "0.1.34" @@ -821,6 +939,8 @@ dependencies = [ "chrono", "chrono-tz", "dotenvy", + "env_logger", + "log", "maud", "rand 0.10.0", "rand 0.8.5", @@ -851,6 +971,12 @@ version = "1.21.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "42f5e15c9953c5e4ccceeb2e7382a716482c34515315f7b03532b8b4e8393d2d" +[[package]] +name = "once_cell_polyfill" +version = "1.70.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "384b8ab6d37215f3c5301a95a4accb5d64aa607f1fcb26a11b5303878451b4fe" + [[package]] name = "parking_lot" version = "0.12.5" @@ -927,6 +1053,21 @@ version = "0.3.32" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7edddbd0b52d732b21ad9a5fab5c704c14cd949e5e9a1ec5929a24fded1b904c" +[[package]] +name = "portable-atomic" +version = "1.13.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c33a9471896f1c69cecef8d20cbe2f7accd12527ce60845ff44c153bb2a21b49" + +[[package]] +name = "portable-atomic-util" +version = "0.2.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7a9db96d7fa8782dd8c15ce32ffe8680bbd1e978a43bf51a34d39483540495f5" +dependencies = [ + "portable-atomic", +] + [[package]] name = "ppv-lite86" version = "0.2.21" @@ -1038,6 +1179,35 @@ dependencies = [ "bitflags", ] +[[package]] +name = "regex" +version = "1.12.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e10754a14b9137dd7b1e3e5b0493cc9171fdd105e0ab477f51b72e7f3ac0e276" +dependencies = [ + "aho-corasick", + "memchr", + "regex-automata", + "regex-syntax", +] + +[[package]] +name = "regex-automata" +version = "0.4.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6e1dd4122fc1595e8162618945476892eefca7b88c52820e74af6262213cae8f" +dependencies = [ + "aho-corasick", + "memchr", + "regex-syntax", +] + +[[package]] +name = "regex-syntax" +version = "0.8.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc897dd8d9e8bd1ed8cdad82b5966c3e0ecae09fb1907d58efaa013543185d0a" + [[package]] name = "rsqlite-vfs" version = "0.1.0" @@ -1440,6 +1610,12 @@ version = "0.2.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ebc1c04c71510c7f702b52b7c350734c9ff1295c464a03335b00bb84fc54f853" +[[package]] +name = "utf8parse" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821" + [[package]] name = "uuid" version = "1.21.0" diff --git a/Cargo.toml b/Cargo.toml index ca0a5cd..f721615 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,6 +11,8 @@ base64 = "0.22.1" chrono = { version = "0.4.43", features = ["serde"] } chrono-tz = "0.10.4" dotenvy = "0.15.7" +env_logger = "0.11.9" +log = "0.4.29" maud = { version = "0.27.0", features = ["axum"] } rand = "0.10.0" rand08 = { version = "0.8.5", package = "rand" } diff --git a/src/config.rs b/src/config.rs new file mode 100644 index 0000000..803942f --- /dev/null +++ b/src/config.rs @@ -0,0 +1,20 @@ +use std::io::{self, Write}; + +use env_logger::fmt::Formatter; +use log::Record; + +pub fn envlogger_write_format(buf: &mut Formatter, rec: &Record) -> io::Result<()> { + let level_string = format!("{}", rec.level()); + let level_style = buf.default_level_style(rec.level()); + write!(buf, "[")?; + write!(buf, "{}", level_style.render_reset())?; + write!(buf, "{}", level_style.render())?; + write!(buf, "{}", level_string)?; + write!(buf, "{}", level_style.render_reset())?; + writeln!( + buf, + " @ {}] {}", + chrono::Local::now().format("%Y-%m-%d %H:%M:%S"), + rec.args() + ) +} diff --git a/src/database/mod.rs b/src/database/mod.rs index 2551888..3429747 100644 --- a/src/database/mod.rs +++ b/src/database/mod.rs @@ -27,7 +27,7 @@ CREATE TABLE IF NOT EXISTS migrations ( pub struct DatabaseError(#[from] rusqlite::Error); impl IntoResponse for DatabaseError { fn into_response(self) -> axum::response::Response { - println!("[DB ERROR] {}", self); + log::error!("[DB ERROR] {}", self); (StatusCode::INTERNAL_SERVER_ERROR, "Internal server error.").into_response() } } @@ -55,14 +55,14 @@ pub fn migrations() -> Result<(), Box> { continue; } changes = true; - println!("Applying migration {key}..."); + log::info!("Applying migration {key}..."); conn.execute_batch(sql)?; conn.execute("INSERT INTO migrations(id) VALUES (?1)", [key])?; } if changes { - println!("Migrations applied.") + log::info!("Migrations applied.") } Ok(()) } diff --git a/src/main.rs b/src/main.rs index 4e53ba6..658d31d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,6 +3,7 @@ use std::error::Error; use tokio::net::TcpListener; mod api; +mod config; mod database; mod persons; mod quotes; @@ -22,6 +23,11 @@ async fn main() -> Result<(), Box> { { return Err(e.into()); } + env_logger::builder() + .filter_level(log::LevelFilter::Info) + .parse_default_env() + .format(config::envlogger_write_format) + .init(); database::migrations()?; users::auth::init_password_dummies(); @@ -36,7 +42,7 @@ async fn main() -> Result<(), Box> { }; let r = api::api_router(); let l = TcpListener::bind(format!("0.0.0.0:{port}")).await?; - println!("Listener bound to {}", l.local_addr()?); + log::info!("Listener bound to {}", l.local_addr()?); axum::serve(l, r).await?; Ok(()) diff --git a/src/tags.rs b/src/tags.rs index 9768ff4..f817864 100644 --- a/src/tags.rs +++ b/src/tags.rs @@ -72,7 +72,7 @@ impl IntoResponse for TagError { fn into_response(self) -> Response { match self { Self::DatabaseError(e) => { - eprintln!("[ERROR] Database error occured: {e}"); + log::error!("[ERROR] Database error occured: {e}"); (StatusCode::INTERNAL_SERVER_ERROR, ISE_MSG.into()) } Self::TagNameError(_) => (StatusCode::BAD_REQUEST, self.to_string()), diff --git a/src/users/auth/implementation.rs b/src/users/auth/implementation.rs index 1c671d9..f61d207 100644 --- a/src/users/auth/implementation.rs +++ b/src/users/auth/implementation.rs @@ -45,11 +45,11 @@ impl IntoResponse for AuthError { Self::InvalidBase64(_) => (StatusCode::BAD_REQUEST, self.to_string()).into_response(), Self::InvalidUtf8(_) => (StatusCode::BAD_REQUEST, self.to_string()).into_response(), Self::DatabaseError(e) => { - eprintln!("[ERROR] Database error occured: {e}"); + log::error!("[ERROR] Database error occured: {e}"); (StatusCode::INTERNAL_SERVER_ERROR, ISE_MSG.to_string()).into_response() } Self::PassHashError(e) => { - eprintln!("[ERROR] A passwordhash error occured: {e}"); + log::error!("[ERROR] A passwordhash error occured: {e}"); (StatusCode::INTERNAL_SERVER_ERROR, ISE_MSG.to_string()).into_response() } } diff --git a/src/users/auth/mod.rs b/src/users/auth/mod.rs index edc6932..309faee 100644 --- a/src/users/auth/mod.rs +++ b/src/users/auth/mod.rs @@ -44,7 +44,7 @@ pub static DUMMY_PASSWORD_PHC: LazyLock = LazyLock::new(|| { }); pub fn init_password_dummies() { let _ = &*DUMMY_PASSWORD_PHC; - println!("Password hashing setup finished"); + log::info!("Password hashing setup finished"); } #[derive(thiserror::Error, Debug)] diff --git a/src/users/mod.rs b/src/users/mod.rs index e05ac2c..31e8322 100644 --- a/src/users/mod.rs +++ b/src/users/mod.rs @@ -154,10 +154,10 @@ impl User { pub fn regenerate_infradmin_password(&mut self) -> Result<(), UserError> { let passw = auth::generate_token(auth::TokenSize::Char16); self.set_password(Some(&passw))?; - println!("[USERS] The infradmin account password has been (re)generated."); - println!("[USERS] Handle: {}", self.handle.as_str()); - println!("[USERS] Password: {}", passw); - println!("[USERS] The infradmin is urged to change this password to a secure one.\n"); + log::info!("[USERS] The infradmin account password has been (re)generated."); + log::info!("[USERS] Handle: {}", self.handle.as_str()); + log::info!("[USERS] Password: {}", passw); + log::info!("[USERS] The infradmin is urged to change this password to a secure one.\n"); Ok(()) } @@ -204,11 +204,11 @@ impl IntoResponse for UserError { fn into_response(self) -> Response { match self { Self::DatabaseError(e) => { - eprintln!("[ERROR] Database error occured: {e}"); + log::error!("[ERROR] Database error occured: {e}"); (StatusCode::INTERNAL_SERVER_ERROR, ISE_MSG.into()) } Self::PassHashError(e) => { - eprintln!("[ERROR] A passwordhash error occured: {e}"); + log::error!("[ERROR] A passwordhash error occured: {e}"); (StatusCode::INTERNAL_SERVER_ERROR, ISE_MSG.into()) } Self::UserHandleError(_) => (StatusCode::BAD_REQUEST, self.to_string()), diff --git a/src/users/sessions.rs b/src/users/sessions.rs index 5dfa4b2..0144f1e 100644 --- a/src/users/sessions.rs +++ b/src/users/sessions.rs @@ -52,7 +52,7 @@ impl IntoResponse for SessionError { fn into_response(self) -> Response { match self { Self::DatabaseError(e) => { - eprintln!("[ERROR] Database error occured: {e}"); + log::error!("[ERROR] Database error occured: {e}"); (StatusCode::INTERNAL_SERVER_ERROR, ISE_MSG.into()) } Self::NoSessionWithId(_) => (StatusCode::BAD_REQUEST, self.to_string()),