Skip to content
This repository was archived by the owner on Sep 21, 2024. It is now read-only.

Commit 8b798fd

Browse files
committed
simplify logging formatters using type erasure.
1 parent 0def4d6 commit 8b798fd

File tree

1 file changed

+31
-42
lines changed

1 file changed

+31
-42
lines changed

rust/noosphere-core/src/tracing.rs

+31-42
Original file line numberDiff line numberDiff line change
@@ -183,10 +183,10 @@ mod inner {
183183
use tracing::{Event, Subscriber};
184184
use tracing_subscriber::{
185185
filter::Directive,
186-
fmt::{format, FmtContext, FormatEvent, FormatFields, FormattedFields, Layer},
186+
fmt::{format, FmtContext, FormatEvent, FormatFields, FormattedFields},
187187
prelude::*,
188188
registry::LookupSpan,
189-
EnvFilter,
189+
EnvFilter, Layer,
190190
};
191191

192192
// Mainly we disable this for iOS because it causes XCode
@@ -317,51 +317,40 @@ mod inner {
317317

318318
let subscriber = tracing_subscriber::registry().with(env_filter);
319319

320-
match noosphere_log_format {
321-
NoosphereLogFormat::Minimal => {
322-
let subscriber = subscriber.with(
323-
Layer::default().event_format(NoosphereMinimalFormatter::new(
324-
tracing_subscriber::fmt::format()
325-
.without_time()
326-
.with_target(false)
327-
.with_ansi(USE_ANSI_COLORS),
328-
)),
329-
);
330-
331-
#[cfg(feature = "sentry")]
332-
let subscriber = subscriber.with(sentry_tracing::layer());
333-
334-
subscriber.init();
335-
}
320+
// Erase layer type via Box<dyn> so that we can share common code even
321+
// if the Layer types are incompatible.
322+
// See: https://docs.rs/tracing-subscriber/latest/tracing_subscriber/layer/#runtime-configuration-with-layers
323+
let formatting_layer: Box<dyn Layer<_> + Send + Sync + 'static> = match noosphere_log_format
324+
{
325+
NoosphereLogFormat::Minimal => Box::new(
326+
tracing_subscriber::fmt::layer().event_format(NoosphereMinimalFormatter::new(
327+
tracing_subscriber::fmt::format()
328+
.without_time()
329+
.with_target(false)
330+
.with_ansi(USE_ANSI_COLORS),
331+
)),
332+
),
336333
NoosphereLogFormat::Verbose => {
337-
let subscriber =
338-
subscriber.with(tracing_subscriber::fmt::layer().with_ansi(USE_ANSI_COLORS));
339-
340-
#[cfg(feature = "sentry")]
341-
let subscriber = subscriber.with(sentry_tracing::layer());
342-
343-
subscriber.init();
334+
Box::new(tracing_subscriber::fmt::layer().with_ansi(USE_ANSI_COLORS))
344335
}
345-
NoosphereLogFormat::Pretty => {
346-
let subscriber =
347-
subscriber.with(Layer::default().pretty().with_ansi(USE_ANSI_COLORS));
348-
349-
#[cfg(feature = "sentry")]
350-
let subscriber = subscriber.with(sentry_tracing::layer());
351-
352-
subscriber.init();
353-
}
354-
NoosphereLogFormat::Structured => {
355-
let subscriber =
356-
subscriber.with(Layer::default().json().with_ansi(USE_ANSI_COLORS));
336+
NoosphereLogFormat::Pretty => Box::new(
337+
tracing_subscriber::fmt::layer()
338+
.pretty()
339+
.with_ansi(USE_ANSI_COLORS),
340+
),
341+
NoosphereLogFormat::Structured => Box::new(
342+
tracing_subscriber::fmt::layer()
343+
.json()
344+
.with_ansi(USE_ANSI_COLORS),
345+
),
346+
};
357347

358-
#[cfg(feature = "sentry")]
359-
let subscriber = subscriber.with(sentry_tracing::layer());
348+
let subscriber = subscriber.with(formatting_layer);
360349

361-
subscriber.init();
362-
}
363-
};
350+
#[cfg(feature = "sentry")]
351+
let subscriber = subscriber.with(sentry_tracing::layer());
364352

353+
subscriber.init();
365354
Ok(())
366355
}
367356

0 commit comments

Comments
 (0)