Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot write custom Layer that uses Format #2988

Open
jsimonrichard opened this issue May 29, 2024 · 4 comments
Open

Cannot write custom Layer that uses Format #2988

jsimonrichard opened this issue May 29, 2024 · 4 comments

Comments

@jsimonrichard
Copy link

Feature Request

I'm new to this crate, to apologies if I'm missing something obvious. But here goes...

I want to create a custom Layer that uses the standard tracing_subscriber::fmt::format::Format formatter.

My plan was to implement Layer::on_event, which is provided a Context. To use the formatter, it seems like I need a FmtContext. However, I can't figure out how to create one of those from a Context. In fact, I haven't found any public functions that return FmtContext.

Crates

tracing-subscriber

Motivation

My particular use case is that I want to send backend logs to the fronted (like in tauri_plugin_log). Each time something is logged I need to call a custom function (which takes a string message input, among other things), but I don't want to write custom formatting for that string.

Proposal

In tracing_subscriber::fmt::Layer, the private make_ctx function is used:

impl<S, N, E, W> Layer<S, N, E, W>
where
    S: Subscriber + for<'a> LookupSpan<'a>,
    N: for<'writer> FormatFields<'writer> + 'static,
    E: FormatEvent<S, N> + 'static,
    W: for<'writer> MakeWriter<'writer> + 'static,
{
    #[inline]
    fn make_ctx<'a>(&'a self, ctx: Context<'a, S>, event: &'a Event<'a>) -> FmtContext<'a, S, N> {
        FmtContext {
            ctx,
            fmt_fields: &self.fmt_fields,
            event,
        }
    }
}

I would like to create a similar function that creates a FmtContext. Maybe something like this:

impl<a, S> Context<'a, S> {
    pub fn to_fmt_context<N>(self, fmt_fields: ?, event: &'a Event<'a>) -> FmtContext<'a, S, N> {
        FmtContext {
            self,
            fmt_fields,
            event
        }
    }
}

Alternatives

I'm not sure. Maybe this could be a function on FmtContext? But I think making this a function on Context makes it clear that this is a conversion from Context to FmtContext which requires extra parameters (fmt_fields and events).

@LHolten
Copy link

LHolten commented Jul 2, 2024

I would also really like this, my use case is turning some errors into panics during tests.

@kaffarell
Copy link
Contributor

Hmm AFAIK this is currently not possible. One workaround would be to use MakeWriter and implement io::Write. https://docs.rs/tracing-subscriber/latest/tracing_subscriber/fmt/struct.Layer.html#method.with_writer

@micahscopes
Copy link

This would be really nice.

@Imberflur
Copy link

Recently ran into this.

There seems to be some relevant discussion in #1039.

The MakeWriter workaround seems like it could work for my case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants