Skip to content

Commit 9e07e79

Browse files
committed
docs: 更新 README.md 和 config.toml 添加日志配置说明
在 README.md 中添加了关于日志配置的详细说明,包括如何在 `config.toml` 中设置日志等级以及如何使用 `RUST_LOG` 环境变量覆盖配置文件中的日志设置。 在 `config.toml` 中添加了 `log_level` 配置项,默认值为 `"info,sqlx::query=warn"`。 在 `crates/common/src/config.rs` 中添加了 `log_level` 字段到 `ServerConfig` 结构体,并定义了默认值函数 `default_log_level`。 在 `src/main.rs` 中调整了初始化 `tracing_subscriber` 的顺序,先加载配置文件以获取日志等级,然后使用该日志等级初始化 `tracing_subscriber`。
1 parent 48aee06 commit 9e07e79

4 files changed

Lines changed: 26 additions & 4 deletions

File tree

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ pnpm dev
7878
[server]
7979
host = "0.0.0.0"
8080
port = 8080
81+
log_level = "info,sqlx::query=warn" # 日志等级(RUST_LOG 语法)
8182

8283
[database]
8384
url = "sqlite:./data/mediadrive.db?mode=rwc" # 或 postgres://user:pass@localhost/mediadrive
@@ -129,6 +130,19 @@ language = "zh-CN" # TMDB 搜索语言
129130
MDP_DATABASE__URL=postgres://... MDP_AUTH__JWT_SECRET=your-secret cargo run
130131
```
131132

133+
日志等级也可用 `RUST_LOG` 环境变量覆盖(优先级高于 config.toml):
134+
135+
```bash
136+
# 调试 SQL 查询
137+
RUST_LOG=info,sqlx::query=info cargo run
138+
139+
# 只看警告和错误
140+
RUST_LOG=warn cargo run
141+
142+
# 调试全部
143+
RUST_LOG=debug cargo run
144+
```
145+
132146
## Web UI
133147

134148
Web 界面包含以下页面:

config.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
[server]
22
host = "0.0.0.0"
33
port = 8080
4+
log_level = "info,sqlx::query=warn"
45

56
[database]
67
url = "sqlite:./data/mediadrive.db?mode=rwc"

crates/common/src/config.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ pub struct AppConfig {
2121
pub struct ServerConfig {
2222
pub host: String,
2323
pub port: u16,
24+
#[serde(default = "default_log_level")]
25+
pub log_level: String,
26+
}
27+
28+
fn default_log_level() -> String {
29+
"info,sqlx::query=warn".to_string()
2430
}
2531

2632
#[derive(Debug, Clone, Deserialize)]

src/main.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,15 @@ use tracing_subscriber::{EnvFilter, layer::SubscriberExt, util::SubscriberInitEx
66

77
#[tokio::main]
88
async fn main() -> Result<(), Box<dyn std::error::Error>> {
9-
// Initialize tracing
9+
// Load configuration first (needed for log_level)
10+
let config = AppConfig::load("config.toml")?;
11+
12+
// Initialize tracing with configured log level
1013
tracing_subscriber::registry()
11-
.with(EnvFilter::try_from_default_env().unwrap_or_else(|_| EnvFilter::new("info")))
14+
.with(EnvFilter::try_from_default_env().unwrap_or_else(|_| EnvFilter::new(&config.server.log_level)))
1215
.with(tracing_subscriber::fmt::layer())
1316
.init();
1417

15-
// Load configuration
16-
let config = AppConfig::load("config.toml")?;
1718
tracing::info!(
1819
"Starting MediaDrivePro on {}:{}",
1920
config.server.host,

0 commit comments

Comments
 (0)