Crates.io | tracing-fn |
lib.rs | tracing-fn |
version | 0.1.0 |
created_at | 2025-09-19 01:15:47.573792+00 |
updated_at | 2025-09-19 01:15:47.573792+00 |
description | 该库提供了一个派生宏, 用于追踪函数的调用参数和返回值 |
homepage | |
repository | https://github.com/mengyou1024/tracing-fn.git |
max_upload_size | |
id | 1845632 |
size | 17,255 |
一个 Rust 过程宏,用于为函数添加 tracing 功能。
在 Cargo.toml
中添加依赖:
[dependencies]
tracing-fn = "..." # 根据实际情况调整路径
tracing = "0.1"
tracing_subscriber = "0.3"
use tracing_fn::tracing_fn;
#[tracing_fn]
fn hello_world(name: &str) -> String {
format!("Hello, {}!", name)
}
#[tracing_fn(level = "info")]
fn add(a: i32, b: i32) -> i32 {
a + b
}
#[tracing_fn(skip = "password")]
fn login(username: &str, password: &str) -> bool {
// password 参数不会被记录
!username.is_empty() && !password.is_empty()
}
在 Release 模式下,默认不启用 tracing 功能。如果需要强制启用,可以使用 force
参数:
#[tracing_fn(force = true)]
fn important_function(a: i32) -> i32 {
a * 2
}
为了使 tracing 正常工作,需要在程序开始时初始化 tracing:
fn main() {
tracing_subscriber::fmt()
.with_max_level(tracing::Level::TRACE)
.init();
// 你的代码
}
# 在调试模式下运行(默认启用 tracing)
cargo run --example example
# 在 Release 模式下运行(默认不启用 tracing)
cargo run --example example --release