Last active: 10 months ago
Rust tracing logger with write to file
use std::env;
use tracing_subscriber::{fmt, prelude::*, registry, EnvFilter};
pub fn init_logger() {
let formatting_layer = fmt::layer()
// .pretty()
.with_thread_ids(false)
.with_target(false)
.with_writer(std::io::stdout);
let env_layer = EnvFilter::try_from_env("RHEA_LOG").unwrap_or_else(|_| {
format!("{}=info,tower_http=info,axum=info", env!("CARGO_PKG_NAME")).into()
});
registry().with(env_layer).with(formatting_layer).init();
}
use crate::config::Config;
use anyhow::Result;
use tracing_appender::{non_blocking::WorkerGuard, rolling};
use tracing_error::ErrorLayer;
use tracing_subscriber::{
filter, fmt, prelude::__tracing_subscriber_SubscriberExt, util::SubscriberInitExt, Registry,
};
pub fn logger_init(config: &Config) -> Result<WorkerGuard> {
let log_path = config.log_path.as_ref().expect("Can not read log path");
let formatting_layer = fmt::layer()
// .pretty()
.with_thread_ids(true)
.with_target(false)
.with_writer(std::io::stdout);
let file_appender = rolling::daily(log_path, "rus-list.log");
let (non_blocking, guard) = tracing_appender::non_blocking(file_appender);
let file_layer = fmt::layer()
.compact()
.with_target(false)
.with_thread_ids(true)
.with_ansi(false)
.with_writer(non_blocking);
let filter = filter::LevelFilter::INFO;
Registry::default()
.with(filter)
.with(ErrorLayer::default())
.with(formatting_layer)
.with(file_layer)
.init();
color_eyre::install().expect("");
Ok(guard)
}
use tracing::level_filters::LevelFilter;
use tracing_subscriber::{fmt, layer::SubscriberExt, registry, util::SubscriberInitExt, EnvFilter};
pub fn init_logger() {
let formatting_layer = fmt::layer()
.with_thread_ids(true)
.with_target(true)
.with_writer(std::io::stdout);
let env_layer = EnvFilter::builder()
.with_default_directive(LevelFilter::INFO.into())
.with_env_var("SPIO_LOG")
.from_env_lossy();
registry().with(env_layer).with(formatting_layer).init();
}