Skip to content

Commit

Permalink
fmt: Support disabling the target in formatted output (#594)
Browse files Browse the repository at this point in the history
This PR adds support for disabling level in formatted output.

Fixes: #592
  • Loading branch information
Kobzol authored Feb 21, 2020
1 parent b8a1d40 commit a457c98
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 7 deletions.
11 changes: 11 additions & 0 deletions tracing-subscriber/src/fmt/fmt_layer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ pub struct Layer<
///
/// let fmt_layer = fmt::Layer::builder()
/// .with_target(false) // don't include event targets when logging
/// .with_level(false) // don't include event levels when logging
/// .finish();
///
/// # let subscriber = fmt_layer.with_subscriber(tracing_subscriber::registry::Registry::default());
Expand Down Expand Up @@ -238,6 +239,16 @@ where
}
}

/// Sets whether or not an event's level is displayed.
pub fn with_level(self, display_level: bool) -> LayerBuilder<S, N, format::Format<L, T>, W> {
LayerBuilder {
fmt_event: self.fmt_event.with_level(display_level),
fmt_fields: self.fmt_fields,
make_writer: self.make_writer,
_inner: self._inner,
}
}

/// Sets the layer being built to use a [less verbose formatter](../fmt/format/struct.Compact.html).
pub fn compact(self) -> LayerBuilder<S, N, format::Format<format::Compact, T>, W>
where
Expand Down
63 changes: 56 additions & 7 deletions tracing-subscriber/src/fmt/format/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ pub struct Format<F = Full, T = SystemTime> {
pub(crate) timer: T,
pub(crate) ansi: bool,
pub(crate) display_target: bool,
pub(crate) display_level: bool,
}

impl Default for Format<Full, SystemTime> {
Expand All @@ -146,6 +147,7 @@ impl Default for Format<Full, SystemTime> {
timer: SystemTime,
ansi: true,
display_target: true,
display_level: true,
}
}
}
Expand All @@ -160,6 +162,7 @@ impl<F, T> Format<F, T> {
timer: self.timer,
ansi: self.ansi,
display_target: self.display_target,
display_level: self.display_level,
}
}

Expand All @@ -174,6 +177,7 @@ impl<F, T> Format<F, T> {
timer: self.timer,
ansi: self.ansi,
display_target: self.display_target,
display_level: self.display_level,
}
}

Expand All @@ -194,6 +198,7 @@ impl<F, T> Format<F, T> {
timer,
ansi: self.ansi,
display_target: self.display_target,
display_level: self.display_level,
}
}

Expand All @@ -204,6 +209,7 @@ impl<F, T> Format<F, T> {
timer: (),
ansi: self.ansi,
display_target: self.display_target,
display_level: self.display_level,
}
}

Expand All @@ -219,6 +225,14 @@ impl<F, T> Format<F, T> {
..self
}
}

/// Sets whether or not an event's level is displayed.
pub fn with_level(self, display_level: bool) -> Format<F, T> {
Format {
display_level,
..self
}
}
}

impl<S, N, T> FormatEvent<S, N> for Format<Full, T>
Expand All @@ -244,21 +258,32 @@ where
#[cfg(not(feature = "ansi"))]
time::write(&self.timer, writer)?;

let (fmt_level, full_ctx) = {
if self.display_level {
let fmt_level = {
#[cfg(feature = "ansi")]
{
FmtLevel::new(meta.level(), self.ansi)
}
#[cfg(not(feature = "ansi"))]
{
FmtLevel::new(meta.level())
}
};
write!(writer, "{} ", fmt_level)?;
}

let full_ctx = {
#[cfg(feature = "ansi")]
{
(
FmtLevel::new(meta.level(), self.ansi),
FullCtx::new(ctx, self.ansi),
)
FullCtx::new(ctx, self.ansi)
}
#[cfg(not(feature = "ansi"))]
{
(FmtLevel::new(meta.level()), FullCtx::new(&ctx))
FullCtx::new(&ctx)
}
};

write!(writer, "{} {}", fmt_level, full_ctx)?;
write!(writer, "{}", full_ctx)?;
if self.display_target {
write!(writer, "{}: ", meta.target())?;
}
Expand Down Expand Up @@ -799,4 +824,28 @@ mod test {
let actual = String::from_utf8(buf.try_lock().unwrap().to_vec()).unwrap();
assert_eq!(expected, actual.as_str());
}

#[test]
fn without_level() {
lazy_static! {
static ref BUF: Mutex<Vec<u8>> = Mutex::new(vec![]);
}

let make_writer = || MockWriter::new(&BUF);
let subscriber = crate::fmt::Subscriber::builder()
.with_writer(make_writer)
.with_level(false)
.with_ansi(false)
.with_timer(MockTime)
.finish();

with_default(subscriber, || {
tracing::info!("hello");
});
let actual = String::from_utf8(BUF.try_lock().unwrap().to_vec()).unwrap();
assert_eq!(
"fake time tracing_subscriber::fmt::format::test: hello\n",
actual.as_str()
);
}
}
11 changes: 11 additions & 0 deletions tracing-subscriber/src/fmt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,17 @@ where
}
}

/// Sets whether or not an event's level is displayed.
pub fn with_level(
self,
display_level: bool,
) -> SubscriberBuilder<N, format::Format<L, T>, F, W> {
SubscriberBuilder {
filter: self.filter,
inner: self.inner.with_level(display_level),
}
}

/// Sets the subscriber being built to use a less verbose formatter.
///
/// See [`format::Compact`].
Expand Down

0 comments on commit a457c98

Please sign in to comment.