Rust语言在Linux系统中的日志管理技巧
log是Rust的标准日志门面(定义info!/error!/debug!/warn!/trace!等宏),env_logger是其常用实现(将日志输出到终端)。
Cargo.toml中添加log = "0.4"和env_logger = "0.10"(最新版本以Crates.io为准)。main.rs中调用env_logger::init()即可启用日志,此时默认输出info及以上级别日志(如info!("Program started"))。RUST_LOG环境变量动态调整级别(如RUST_LOG=debug cargo run输出debug及以上级别,RUST_LOG=error仅输出错误日志),无需修改代码。默认格式较简单,可通过Builder自定义输出样式(如添加时间戳、对齐级别)。
Cargo.toml中加入chrono = "0.4"(用于生成时间戳)。use env_logger::Builder;
use log::Level;
use chrono::Local;
fn main() {
Builder::from_env(env_logger::Env::default().default_filter_or("info"))
.format(|buf, record| {
writeln!(
buf,
"{} [{}] - {}",
Local::now().format("%Y-%m-%d %H:%M:%S"), // 时间戳
record.level(), // 日志级别
record.args() // 日志内容
)
})
.init();
info!("Custom formatted log");
}
输出示例:2025-09-29 14:30:00 [INFO] - Custom formatted log。对于生产环境,可使用log4rs(类似Java的Logback)实现日志文件输出、轮转及分级管理。
log4rs.yaml:refresh_rate: 30 seconds # 配置刷新间隔
appenders:
stdout: # 控制台输出
kind: console
file: # 文件输出(带轮转)
kind: rolling_file
path: "logs/app.log"
policy: compound # 复合策略(大小+时间)
trigger:
kind: size
max_size: 10MB
roller:
kind: delete
keep: 5 # 保留最近5个文件
encoder:
pattern: "{d(%Y-%m-%d %H:%M:%S)} [{l}] {m}{n}" # 格式:时间+级别+内容
root:
level: info
appenders:
- stdout
- file
loggers:
app::database: # 特定模块日志级别
level: debug
appenders:
- file
additive: false # 不继承root的appender
use log::info;
fn main() {
log4rs::init_file("config/log4rs.yaml", Default::default()).unwrap();
info!("This log will be written to file and console");
// 数据库模块日志(debug级别,仅输出到文件)
log::logger().with_max_level(Level::Debug).log(
&log::Record::builder()
.module_path(Some("app::database"))
.args(format_args!("Database connected"))
.build(),
);
}
此配置会将日志输出到logs/app.log,当日志文件超过10MB时自动轮转,保留最近5个文件。serde_json记录JSON格式日志(便于ELK、Loki等工具分析),例如:use log::info;
use serde_json::json;
fn main() {
let user = "Alice";
let action = "login";
info!("{}", json!({ "user": user, "action": action, "status": "success" }));
}
输出:{"user":"Alice","action":"login","status":"success"}。use reqwest::Client;
use serde_json::json;
#[tokio::main]
async fn main() -> Result<(), reqwest::Error> {
let client = Client::new();
let log_data = json![
{ "stream": "app-logs", "logs": [{"timestamp": "2025-09-29T14:30:00Z", "message": "User logged in"}] }
];
client.post("http://openobserve-api.yourdomain.com/v1/logs")
.body(log_data.to_string())
.send()
.await?;
Ok(())
}
需提前部署OpenObserve(通过helm install命令)。debug或trace,输出详细信息(如函数调用、变量值),便于调试。info,输出关键流程信息(如接口调用、数据库操作),避免过多调试日志。warn或error,仅输出异常和重要警告(减少磁盘占用),同时通过log4rs等工具将日志存储到集中式系统(如OpenObserve)。