Request Id
Request Id 中間件是比較靈活的,生成器(IdGenerator) 用於生成 ID, 可以定義自己的 ID 生成器,隻要實現 IdGenerator
這個 trait. 默認提供生成器是 UlidGenerator
.
另外你可以控製是否覆蓋現在的已經存在的 requestid
. 還可以設置 header_name
等, 具體請查看文檔.
示例代碼
use salvo::logging::Logger;
use salvo::prelude::*;
#[handler]
async fn hello() -> &'static str {
"Hello World"
}
#[tokio::main]
async fn main() {
tracing_subscriber::fmt().init();
let router = Router::new().get(hello);
let service = Service::new(router).hoop(Logger::new());
let acceptor = TcpListener::new("0.0.0.0:5800").bind().await;
Server::new(acceptor).serve(service).await;
}
[package]
name = "example-logging"
version = "0.1.0"
edition = "2021"
publish = false
[dependencies]
salvo = { workspace = true, features = ["logging"] }
tokio = { version = "1", features = ["macros"] }
tracing = "0.1"
tracing-subscriber = "0.3"