gpt4 book ai didi

rust - 如何在跟踪中使用动态范围名称?

转载 作者:行者123 更新时间:2023-12-03 11:30:28 24 4
gpt4 key购买 nike

我需要动态地使用不同的名称来显示跨度。如何使用动态命名创建跟踪范围?

use tracing; // 0.1.22

fn main() {
let name: &'static str = "foo bar";
let span = tracing::span!(tracing::Level::TRACE, name);
let span_handle = span.enter();
tracing::info!("Hello, world!");
drop(span_handle);
}
error[E0435]: attempt to use a non-constant value in a constant
--> src/main.rs:5:54
|
5 | let span = tracing::span!(tracing::Level::TRACE, name);
| ^^^^ non-constant value
Playground

最佳答案

而不是尝试动态设置跨度的名称,而是添加一个字段:

let value = "forty-two";
let span = tracing::span!(tracing::Level::TRACE, "foo bar", value = value);
the docs:

A span consists of fields, user-defined key-value pairs of arbitrary data that describe the context the span represents

关于rust - 如何在跟踪中使用动态范围名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65735619/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com