Skip to content
Snippets Groups Projects
Commit b8791256 authored by Nick Mathewson's avatar Nick Mathewson :game_die:
Browse files

Disable safe-logging when logging to console.

parent 7e34692a
No related branches found
No related tags found
No related merge requests found
......@@ -145,6 +145,28 @@ fn filt_from_opt_str(s: &Option<String>, source: &str) -> Result<Option<Targets>
})
}
/// Helper to disable safe-logging when formatting an event to be logged to the
/// console.
struct FormatWithSafeLoggingSuppressed<F> {
/// An inner formatting type that does the actual formatting.
inner: F,
}
impl<S, N, F> fmt::FormatEvent<S, N> for FormatWithSafeLoggingSuppressed<F>
where
F: fmt::FormatEvent<S, N>,
N: for<'writer> fmt::FormatFields<'writer> + 'static,
S: Subscriber + for<'span> tracing_subscriber::registry::LookupSpan<'span>,
{
fn format_event(
&self,
ctx: &fmt::FmtContext<'_, S, N>,
writer: fmt::format::Writer<'_>,
event: &tracing::Event<'_>,
) -> std::fmt::Result {
safelog::with_safe_logging_suppressed(|| self.inner.format_event(ctx, writer, event))
}
}
/// Try to construct a tracing [`Layer`] for logging to stdout.
fn console_layer<S>(config: &LoggingConfig, cli: Option<&str>) -> Result<impl Layer<S>>
where
......@@ -154,7 +176,14 @@ where
.map(|s| filt_from_str_verbose(s, "--log-level command line parameter"))
.or_else(|| filt_from_opt_str(&config.console, "logging.console").transpose())
.unwrap_or_else(|| Ok(Targets::from_str("debug").expect("bad default")))?;
Ok(fmt::Layer::default().with_filter(filter))
// We suppress safe logging when formatting messages for the console,
// which we assume to be volatile.
let format = FormatWithSafeLoggingSuppressed {
inner: fmt::format(),
};
Ok(fmt::Layer::default()
.event_format(format)
.with_filter(filter))
}
/// Try to construct a tracing [`Layer`] for logging to journald, if one is
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment