mirror of
https://github.com/cloudflare/workers-rs.git
synced 2025-12-08 18:01:59 +00:00
fix panic in tracing example
Multiple calls to init will cause a panic because a tracing subscriber has already been set globally.
This commit is contained in:
parent
3e451c42b9
commit
56513d9534
@ -10,8 +10,10 @@ fn test() {
|
||||
tracing::warn!(foo = "bar", "baz");
|
||||
}
|
||||
|
||||
#[event(fetch)]
|
||||
async fn main(req: Request, _env: Env, _ctx: Context) -> Result<Response> {
|
||||
// Multiple calls to `init` will cause a panic as a tracing subscriber is already set.
|
||||
// So we use the `start` event to initialize our tracing subscriber when the worker starts.
|
||||
#[event(start)]
|
||||
fn start() {
|
||||
let fmt_layer = tracing_subscriber::fmt::layer()
|
||||
.json()
|
||||
.with_ansi(false) // Only partially supported across JavaScript runtimes
|
||||
@ -22,7 +24,10 @@ async fn main(req: Request, _env: Env, _ctx: Context) -> Result<Response> {
|
||||
.with(fmt_layer)
|
||||
.with(perf_layer)
|
||||
.init();
|
||||
}
|
||||
|
||||
#[event(fetch)]
|
||||
async fn main(req: Request, _env: Env, _ctx: Context) -> Result<Response> {
|
||||
tracing::info!(request=?req, "Handling request");
|
||||
test();
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user