Expand description

Tracing subscriber for emitting logs into Cloudflare’s syslog pipeline (queryable via Kibana).

Follow How To: Generate a WShim Token to generate a logging token and put it in your worker secrets:

wrangler secret put LOGGING_TOKEN

Usage in worker using workers-rs:

use tracing::{debug, info, info_span, Instrument};
use worker::*;
use worker_cf_logging::initialize_tracing;

#[event(fetch)]
pub async fn main(req: Request, env: Env, ctx: worker::Context) -> Result<Response> {
    let logging_token = env.secret("LOGGING_TOKEN")?.to_string();

    let log_level = match env.var("LOG_LEVEL") {
        Ok(var) => var.to_string(),
        Err(_) => "info".to_string(),
    };

    let logger = initialize_tracing(&log_level, logging_token, true);

    let res = handle(req).instrument(info_span!("handle")).await?;

    ctx.wait_until(async move {
        _ = logger.flush().await;
    });

    Ok(res)
}

async fn handle(req: Request) -> Result<Response> {
    info!(path = req.path(), req_method = ?req.method(), "incoming request");

    let req = worker::Fetch::Url(Url::parse("https://httpbin.org/ip")?);

    let ip = req.send().await?.text().await?;

    debug!(
        ip_response = ?ip,
        veg = "potato",
        float = 0.12345,
        signed = -12345,
        unsigned = 12345u64,
        flag = false,
        "my favourites"
    );

    Response::ok("bienvenue")
}

You can then query your logs in Kibana, the service being the one you specified when generating the logging token.

Structs

Functions