fix: 日志输出从文件改为 stdout(journald 可见),紧凑格式,去掉 tracing-appender
This commit is contained in:
32
src/main.rs
32
src/main.rs
@@ -154,7 +154,7 @@ async fn main() -> std::io::Result<()> {
|
||||
// Sentry 初始化(仅在 SENTRY_DSN 环境变量已设置时启用)
|
||||
let _sentry_guard = if let Ok(dsn) = std::env::var("SENTRY_DSN") {
|
||||
if !dsn.is_empty() {
|
||||
eprintln!("[SENTRY] Sentry 已初始化 (DSN: {})", dsn);
|
||||
info!("Sentry 已初始化");
|
||||
Some(sentry::init((
|
||||
dsn,
|
||||
sentry::ClientOptions {
|
||||
@@ -167,30 +167,14 @@ async fn main() -> std::io::Result<()> {
|
||||
},
|
||||
)))
|
||||
} else {
|
||||
eprintln!("[SENTRY] SENTRY_DSN 为空,跳过 Sentry 初始化");
|
||||
info!("SENTRY_DSN 为空,跳过 Sentry 初始化");
|
||||
None
|
||||
}
|
||||
} else {
|
||||
eprintln!("[SENTRY] 未设置 SENTRY_DSN,跳过 Sentry 初始化");
|
||||
info!("未设置 SENTRY_DSN,跳过 Sentry 初始化");
|
||||
None
|
||||
};
|
||||
|
||||
// 初始化文件日志(JSON 格式,带轮转)
|
||||
let log_dir = std::path::Path::new("./logs");
|
||||
std::fs::create_dir_all(log_dir).ok(); // 确保日志目录存在
|
||||
|
||||
let file_appender = tracing_appender::rolling::Builder::new()
|
||||
.rotation(tracing_appender::rolling::Rotation::DAILY)
|
||||
.filename_prefix("rust-backend")
|
||||
.filename_suffix("log")
|
||||
.build(log_dir)
|
||||
.expect("无法创建日志文件");
|
||||
|
||||
let (non_blocking, _guard) = tracing_appender::non_blocking(file_appender);
|
||||
|
||||
// 保持文件 guard 存活(使用 Box 泄漏)
|
||||
std::mem::forget(_guard);
|
||||
|
||||
use std::str::FromStr;
|
||||
let default_directive = tracing_subscriber::filter::Directive::from_str("info").unwrap();
|
||||
|
||||
@@ -213,14 +197,14 @@ async fn main() -> std::io::Result<()> {
|
||||
)
|
||||
)
|
||||
.with_target(true)
|
||||
.with_thread_ids(false) // 生产环境可开启
|
||||
.with_thread_ids(false)
|
||||
.with_file(true)
|
||||
.with_line_number(true)
|
||||
.with_writer(non_blocking)
|
||||
.with_ansi(false) // 文件中不使用 ANSI 颜色
|
||||
.json() // JSON 格式便于 ELK 解析
|
||||
.with_ansi(true)
|
||||
.compact()
|
||||
.init();
|
||||
info!("日志系统初始化成功,JSON格式,输出到 ./logs/");
|
||||
info!("日志系统初始化成功,输出到 systemd journal (journalctl)");
|
||||
info!("日志级别: {}", app_config.rust_log);
|
||||
|
||||
let app_state = match AppState::load() {
|
||||
Ok(state) => state,
|
||||
|
||||
Reference in New Issue
Block a user