From 1a359f8d84d9a306d4e5e1aa387269883a1c0a1a Mon Sep 17 00:00:00 2001 From: milky0217 Date: Mon, 20 Jul 2026 09:24:12 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E6=97=A5=E5=BF=97=E8=BE=93=E5=87=BA?= =?UTF-8?q?=E4=BB=8E=E6=96=87=E4=BB=B6=E6=94=B9=E4=B8=BA=20stdout=EF=BC=88?= =?UTF-8?q?journald=20=E5=8F=AF=E8=A7=81=EF=BC=89=EF=BC=8C=E7=B4=A7?= =?UTF-8?q?=E5=87=91=E6=A0=BC=E5=BC=8F=EF=BC=8C=E5=8E=BB=E6=8E=89=20tracin?= =?UTF-8?q?g-appender?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Cargo.lock | 22 ---------------------- Cargo.toml | 1 - src/main.rs | 32 ++++++++------------------------ 3 files changed, 8 insertions(+), 47 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index b266e26..707515d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -544,15 +544,6 @@ dependencies = [ "cfg-if", ] -[[package]] -name = "crossbeam-channel" -version = "0.5.15" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "82b8f8f868b36967f9606790d1903570de9ceaf870a7bf9fbbd3016d636a2cb2" -dependencies = [ - "crossbeam-utils", -] - [[package]] name = "crossbeam-queue" version = "0.3.12" @@ -2358,7 +2349,6 @@ dependencies = [ "sqlx", "tokio", "tracing", - "tracing-appender", "tracing-subscriber", "uuid", ] @@ -3304,18 +3294,6 @@ dependencies = [ "tracing-core", ] -[[package]] -name = "tracing-appender" -version = "0.2.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "786d480bce6247ab75f005b14ae1624ad978d3029d9113f0a22fa1ac773faeaf" -dependencies = [ - "crossbeam-channel", - "thiserror", - "time", - "tracing-subscriber", -] - [[package]] name = "tracing-attributes" version = "0.1.30" diff --git a/Cargo.toml b/Cargo.toml index 59be19b..520612b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,7 +9,6 @@ chrono = {version = "0.4.41", features=["serde"]} dotenvy = "0.15.7" tracing = "0.1" tracing-subscriber = { version = "0.3", features = ["env-filter", "json"] } -tracing-appender = "0.2" sentry = "0.48" sentry-actix = "0.48" sentry-tracing = "0.48" diff --git a/src/main.rs b/src/main.rs index 2a00402..9c2cd9a 100644 --- a/src/main.rs +++ b/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,