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:
Zeb Piasecki 2023-06-27 14:40:17 -04:00
parent 3e451c42b9
commit 56513d9534

View File

@ -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();