switch logging over to log & env_logger crate

This commit is contained in:
2026-03-02 15:52:18 +01:00
parent f6a9807794
commit da5300b713
10 changed files with 219 additions and 15 deletions

176
Cargo.lock generated
View File

@@ -8,6 +8,15 @@ version = "2.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "320119579fcad9c21884f5c4861d16174d0e06250625266f50fe6898340abefa" 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]] [[package]]
name = "alloc-no-stdlib" name = "alloc-no-stdlib"
version = "2.0.4" version = "2.0.4"
@@ -32,6 +41,56 @@ dependencies = [
"libc", "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]] [[package]]
name = "anyhow" name = "anyhow"
version = "1.0.102" version = "1.0.102"
@@ -260,6 +319,12 @@ dependencies = [
"phf", "phf",
] ]
[[package]]
name = "colorchoice"
version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b05b61dc5112cbb17e4b6cd61790d9845d13888356391624cbe7e41efeac1e75"
[[package]] [[package]]
name = "compression-codecs" name = "compression-codecs"
version = "0.4.37" version = "0.4.37"
@@ -340,6 +405,29 @@ version = "0.15.7"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1aaf95b3e5c8f23aa320147307562d361db0ae0d51242340f558153b4eb2439b" 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]] [[package]]
name = "equivalent" name = "equivalent"
version = "1.0.2" version = "1.0.2"
@@ -673,12 +761,42 @@ dependencies = [
"serde", "serde",
] ]
[[package]]
name = "is_terminal_polyfill"
version = "1.70.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a6cb138bb79a146c1bd460005623e142ef0181e3d0219cb493e02f7d08a35695"
[[package]] [[package]]
name = "itoa" name = "itoa"
version = "1.0.17" version = "1.0.17"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "92ecc6618181def0457392ccd0ee51198e065e016d1d527a7ac1b6dc7c1f09d2" 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]] [[package]]
name = "jobserver" name = "jobserver"
version = "0.1.34" version = "0.1.34"
@@ -821,6 +939,8 @@ dependencies = [
"chrono", "chrono",
"chrono-tz", "chrono-tz",
"dotenvy", "dotenvy",
"env_logger",
"log",
"maud", "maud",
"rand 0.10.0", "rand 0.10.0",
"rand 0.8.5", "rand 0.8.5",
@@ -851,6 +971,12 @@ version = "1.21.3"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "42f5e15c9953c5e4ccceeb2e7382a716482c34515315f7b03532b8b4e8393d2d" checksum = "42f5e15c9953c5e4ccceeb2e7382a716482c34515315f7b03532b8b4e8393d2d"
[[package]]
name = "once_cell_polyfill"
version = "1.70.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "384b8ab6d37215f3c5301a95a4accb5d64aa607f1fcb26a11b5303878451b4fe"
[[package]] [[package]]
name = "parking_lot" name = "parking_lot"
version = "0.12.5" version = "0.12.5"
@@ -927,6 +1053,21 @@ version = "0.3.32"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7edddbd0b52d732b21ad9a5fab5c704c14cd949e5e9a1ec5929a24fded1b904c" 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]] [[package]]
name = "ppv-lite86" name = "ppv-lite86"
version = "0.2.21" version = "0.2.21"
@@ -1038,6 +1179,35 @@ dependencies = [
"bitflags", "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]] [[package]]
name = "rsqlite-vfs" name = "rsqlite-vfs"
version = "0.1.0" version = "0.1.0"
@@ -1440,6 +1610,12 @@ version = "0.2.6"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ebc1c04c71510c7f702b52b7c350734c9ff1295c464a03335b00bb84fc54f853" checksum = "ebc1c04c71510c7f702b52b7c350734c9ff1295c464a03335b00bb84fc54f853"
[[package]]
name = "utf8parse"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821"
[[package]] [[package]]
name = "uuid" name = "uuid"
version = "1.21.0" version = "1.21.0"

View File

@@ -11,6 +11,8 @@ base64 = "0.22.1"
chrono = { version = "0.4.43", features = ["serde"] } chrono = { version = "0.4.43", features = ["serde"] }
chrono-tz = "0.10.4" chrono-tz = "0.10.4"
dotenvy = "0.15.7" dotenvy = "0.15.7"
env_logger = "0.11.9"
log = "0.4.29"
maud = { version = "0.27.0", features = ["axum"] } maud = { version = "0.27.0", features = ["axum"] }
rand = "0.10.0" rand = "0.10.0"
rand08 = { version = "0.8.5", package = "rand" } rand08 = { version = "0.8.5", package = "rand" }

20
src/config.rs Normal file
View File

@@ -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()
)
}

View File

@@ -27,7 +27,7 @@ CREATE TABLE IF NOT EXISTS migrations (
pub struct DatabaseError(#[from] rusqlite::Error); pub struct DatabaseError(#[from] rusqlite::Error);
impl IntoResponse for DatabaseError { impl IntoResponse for DatabaseError {
fn into_response(self) -> axum::response::Response { 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() (StatusCode::INTERNAL_SERVER_ERROR, "Internal server error.").into_response()
} }
} }
@@ -55,14 +55,14 @@ pub fn migrations() -> Result<(), Box<dyn Error>> {
continue; continue;
} }
changes = true; changes = true;
println!("Applying migration {key}..."); log::info!("Applying migration {key}...");
conn.execute_batch(sql)?; conn.execute_batch(sql)?;
conn.execute("INSERT INTO migrations(id) VALUES (?1)", [key])?; conn.execute("INSERT INTO migrations(id) VALUES (?1)", [key])?;
} }
if changes { if changes {
println!("Migrations applied.") log::info!("Migrations applied.")
} }
Ok(()) Ok(())
} }

View File

@@ -3,6 +3,7 @@ use std::error::Error;
use tokio::net::TcpListener; use tokio::net::TcpListener;
mod api; mod api;
mod config;
mod database; mod database;
mod persons; mod persons;
mod quotes; mod quotes;
@@ -22,6 +23,11 @@ async fn main() -> Result<(), Box<dyn Error>> {
{ {
return Err(e.into()); return Err(e.into());
} }
env_logger::builder()
.filter_level(log::LevelFilter::Info)
.parse_default_env()
.format(config::envlogger_write_format)
.init();
database::migrations()?; database::migrations()?;
users::auth::init_password_dummies(); users::auth::init_password_dummies();
@@ -36,7 +42,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
}; };
let r = api::api_router(); let r = api::api_router();
let l = TcpListener::bind(format!("0.0.0.0:{port}")).await?; 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?; axum::serve(l, r).await?;
Ok(()) Ok(())

View File

@@ -72,7 +72,7 @@ impl IntoResponse for TagError {
fn into_response(self) -> Response { fn into_response(self) -> Response {
match self { match self {
Self::DatabaseError(e) => { Self::DatabaseError(e) => {
eprintln!("[ERROR] Database error occured: {e}"); log::error!("[ERROR] Database error occured: {e}");
(StatusCode::INTERNAL_SERVER_ERROR, ISE_MSG.into()) (StatusCode::INTERNAL_SERVER_ERROR, ISE_MSG.into())
} }
Self::TagNameError(_) => (StatusCode::BAD_REQUEST, self.to_string()), Self::TagNameError(_) => (StatusCode::BAD_REQUEST, self.to_string()),

View File

@@ -45,11 +45,11 @@ impl IntoResponse for AuthError {
Self::InvalidBase64(_) => (StatusCode::BAD_REQUEST, self.to_string()).into_response(), Self::InvalidBase64(_) => (StatusCode::BAD_REQUEST, self.to_string()).into_response(),
Self::InvalidUtf8(_) => (StatusCode::BAD_REQUEST, self.to_string()).into_response(), Self::InvalidUtf8(_) => (StatusCode::BAD_REQUEST, self.to_string()).into_response(),
Self::DatabaseError(e) => { 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() (StatusCode::INTERNAL_SERVER_ERROR, ISE_MSG.to_string()).into_response()
} }
Self::PassHashError(e) => { 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() (StatusCode::INTERNAL_SERVER_ERROR, ISE_MSG.to_string()).into_response()
} }
} }

View File

@@ -44,7 +44,7 @@ pub static DUMMY_PASSWORD_PHC: LazyLock<String> = LazyLock::new(|| {
}); });
pub fn init_password_dummies() { pub fn init_password_dummies() {
let _ = &*DUMMY_PASSWORD_PHC; let _ = &*DUMMY_PASSWORD_PHC;
println!("Password hashing setup finished"); log::info!("Password hashing setup finished");
} }
#[derive(thiserror::Error, Debug)] #[derive(thiserror::Error, Debug)]

View File

@@ -154,10 +154,10 @@ impl User {
pub fn regenerate_infradmin_password(&mut self) -> Result<(), UserError> { pub fn regenerate_infradmin_password(&mut self) -> Result<(), UserError> {
let passw = auth::generate_token(auth::TokenSize::Char16); let passw = auth::generate_token(auth::TokenSize::Char16);
self.set_password(Some(&passw))?; self.set_password(Some(&passw))?;
println!("[USERS] The infradmin account password has been (re)generated."); log::info!("[USERS] The infradmin account password has been (re)generated.");
println!("[USERS] Handle: {}", self.handle.as_str()); log::info!("[USERS] Handle: {}", self.handle.as_str());
println!("[USERS] Password: {}", passw); log::info!("[USERS] Password: {}", passw);
println!("[USERS] The infradmin is urged to change this password to a secure one.\n"); log::info!("[USERS] The infradmin is urged to change this password to a secure one.\n");
Ok(()) Ok(())
} }
@@ -204,11 +204,11 @@ impl IntoResponse for UserError {
fn into_response(self) -> Response { fn into_response(self) -> Response {
match self { match self {
Self::DatabaseError(e) => { Self::DatabaseError(e) => {
eprintln!("[ERROR] Database error occured: {e}"); log::error!("[ERROR] Database error occured: {e}");
(StatusCode::INTERNAL_SERVER_ERROR, ISE_MSG.into()) (StatusCode::INTERNAL_SERVER_ERROR, ISE_MSG.into())
} }
Self::PassHashError(e) => { 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()) (StatusCode::INTERNAL_SERVER_ERROR, ISE_MSG.into())
} }
Self::UserHandleError(_) => (StatusCode::BAD_REQUEST, self.to_string()), Self::UserHandleError(_) => (StatusCode::BAD_REQUEST, self.to_string()),

View File

@@ -52,7 +52,7 @@ impl IntoResponse for SessionError {
fn into_response(self) -> Response { fn into_response(self) -> Response {
match self { match self {
Self::DatabaseError(e) => { Self::DatabaseError(e) => {
eprintln!("[ERROR] Database error occured: {e}"); log::error!("[ERROR] Database error occured: {e}");
(StatusCode::INTERNAL_SERVER_ERROR, ISE_MSG.into()) (StatusCode::INTERNAL_SERVER_ERROR, ISE_MSG.into())
} }
Self::NoSessionWithId(_) => (StatusCode::BAD_REQUEST, self.to_string()), Self::NoSessionWithId(_) => (StatusCode::BAD_REQUEST, self.to_string()),