fix: 日志输出从文件改为 stdout(journald 可见),紧凑格式,去掉 tracing-appender

This commit is contained in:
2026-07-20 09:24:12 +08:00
parent d9c9d1dca0
commit 1a359f8d84
3 changed files with 8 additions and 47 deletions

22
Cargo.lock generated
View File

@@ -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"

View File

@@ -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"

View File

@@ -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,