switch logging over to log & env_logger crate
This commit is contained in:
20
src/config.rs
Normal file
20
src/config.rs
Normal 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()
|
||||
)
|
||||
}
|
||||
@@ -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<dyn Error>> {
|
||||
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(())
|
||||
}
|
||||
|
||||
@@ -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<dyn Error>> {
|
||||
{
|
||||
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<dyn Error>> {
|
||||
};
|
||||
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(())
|
||||
|
||||
@@ -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()),
|
||||
|
||||
@@ -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()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -44,7 +44,7 @@ pub static DUMMY_PASSWORD_PHC: LazyLock<String> = 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)]
|
||||
|
||||
@@ -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()),
|
||||
|
||||
@@ -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()),
|
||||
|
||||
Reference in New Issue
Block a user