74 lines
1.9 KiB
Rust
74 lines
1.9 KiB
Rust
use std::str::FromStr;
|
|
|
|
use chrono::Local;
|
|
use anyhow::Result;
|
|
use tracing::info;
|
|
|
|
/// # Fn `setup_logger`
|
|
///
|
|
/// ## function to init terminal logger
|
|
///
|
|
/// ### Dev-Info :
|
|
///
|
|
/// *input* : -
|
|
///
|
|
/// *output* : `anyhow::Result<()>`
|
|
///
|
|
/// *initiator* : fn `main`
|
|
///
|
|
/// *managing* : -
|
|
///
|
|
/// *depends on* : -
|
|
///
|
|
pub async fn setup_logger() -> Result<()> {
|
|
// Builder::new()
|
|
// .format(move |buf, record| {
|
|
// writeln!(
|
|
// buf,
|
|
// "|{}| {} [{}] - {}",
|
|
// "api-grubber",
|
|
// Local::now().format("%d-%m-%Y %H:%M:%S"),
|
|
// record.level(),
|
|
// record.args(),
|
|
// )
|
|
// })
|
|
// .filter(None, LevelFilter::Info)
|
|
// .target(env_logger::Target::Stdout)
|
|
// .init();
|
|
let log_level = std::env::var("IM_LOG_INFO").unwrap_or_else(|_| String::from("INFO"));
|
|
|
|
tracing_subscriber::fmt()
|
|
.with_max_level(
|
|
tracing::Level::from_str(&log_level)
|
|
.unwrap_or_else(|_| tracing::Level::INFO))
|
|
.with_writer(std::io::stdout)
|
|
.with_span_events(tracing_subscriber::fmt::format::FmtSpan::NEW)
|
|
// .with_timer(Local::now().format("%d-%m-%Y %H:%M:%S"))
|
|
.with_line_number(false)
|
|
.with_target(false)
|
|
.with_file(false)
|
|
.compact()
|
|
.init();
|
|
|
|
info!("Logger configured");
|
|
Ok(())
|
|
}
|
|
|
|
|
|
#[cfg(test)]
|
|
mod logger_unittests {
|
|
use tokio::test;
|
|
#[test]
|
|
async fn check_logger_builder() {
|
|
tracing_subscriber::fmt()
|
|
.with_max_level(tracing::Level::INFO)
|
|
.with_test_writer()
|
|
.with_span_events(tracing_subscriber::fmt::format::FmtSpan::NEW)
|
|
// .with_timer(Local::now().format("%d-%m-%Y %H:%M:%S"))
|
|
.with_line_number(false)
|
|
.with_target(false)
|
|
.with_file(false)
|
|
.compact()
|
|
.init();
|
|
}
|
|
} |