branch: main
lib.rs
1177 bytesRaw
use tracing_subscriber::fmt::format::Pretty;
use tracing_subscriber::fmt::time::UtcTime;
use tracing_subscriber::prelude::*;
use tracing_web::{performance_layer, MakeConsoleWriter};
use worker::*;
#[tracing::instrument]
fn test() {
tracing::warn!(foo = "bar", "baz");
}
// 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
.with_timer(UtcTime::rfc_3339()) // std::time is not available in browsers
.with_writer(MakeConsoleWriter); // write events to the console
let perf_layer = performance_layer().with_details_from_fields(Pretty::default());
tracing_subscriber::registry()
.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();
Response::ok("Hello, World!")
}