tokio-rs / tracing

Application level tracing for Rust.
https://tracing.rs
MIT License
5.48k stars 721 forks source link

Filter to detect/exclude events sourced from the `log` crate via `tracing-log` #3059

Open lcmgh opened 2 months ago

lcmgh commented 2 months ago

Feature Request

Crates

Motivation

When using tracing with tracing-log to convert log crate entries to tracing events I'd like to exclude such events from being exported to my tracing backend. For filtering one can for instance utilize Metadata but I can't find any option to tell whether an event was sourced from a log entry. So I currently must export all my logs to the tracing backend which is what I want to prevent.

Proposal

Alternatives

I found tracing quite useful as primary logger due to it's layer system. Maybe I am using the wrong tools here and should rather take a regular logger and then convert tracing events to logs?

kaffarell commented 2 months ago

Hmm this is kind of a hacky solution, but:

    tracing_subscriber::registry()
        .with(tracing_subscriber::fmt::layer())
        .with(filter_fn(|meta| {
            meta.fields().field("log.target").is_none()
        }))
        .init();

    log::debug!("this is a log line");
    tracing::debug!("this is a tracing line");

log-events do have some metadata that distinguish them: log.target, log.module_path, log.file, log.line. But this is not really a fool-proof solution. Maybe we could add a specific field like: source: log or something?