Skip to content

Commit

Permalink
ref: 优化日志输出
Browse files Browse the repository at this point in the history
  • Loading branch information
SerenitySir committed Aug 20, 2024
1 parent 52d0b1d commit 305b62e
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ fn main() {
debug!("config: {config:#?}");
let level_filter =
LevelFilter::from_str(config.log.level.as_str()).unwrap_or(LevelFilter::Info);
logs::init(pkg_name.to_owned() + ".log", level_filter).expect("初始化日志出错");
logs::init(pkg_name.to_owned(), level_filter).expect("初始化日志出错");

let cli = cmd::Cli::parse();

Expand Down
14 changes: 8 additions & 6 deletions src/util/logs.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use log::LevelFilter;
use std::io::Error;
use std::str::FromStr;
use std::sync::Mutex;
use std::{fs};
use std::fs;
use tracing::Level;
use tracing_subscriber::fmt::writer::MakeWriterExt;
use tracing_subscriber::util::SubscriberInitExt;
Expand Down Expand Up @@ -171,10 +170,13 @@ fn init_tracing(logs_dir: String, log_file: String, level: LevelFilter) {
} else {
builder
//非调试模式输出到日志文件
.with_writer(Mutex::new(tracing_appender::rolling::daily(
logs_dir.clone(),
log_file.clone(),
)))
.with_writer(tracing_appender::rolling::Builder::new()
.filename_prefix(log_file.clone())
.filename_suffix("log")
.max_log_files(7)
.rotation(tracing_appender::rolling::Rotation::DAILY)
.build(logs_dir.clone())
.unwrap())
.finish()
.init();
}
Expand Down

0 comments on commit 305b62e

Please sign in to comment.