Skip to content

Allow configure fmt::Layer better (#353) #356

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

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 18 additions & 16 deletions src/tracing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,32 +38,34 @@ where
Wr: Writer<W>,
Cli: clap::Args,
{
/// Initializes a global [`tracing::Subscriber`] with a default
/// [`fmt::Layer`] and [`LevelFilter::INFO`].
/// Initializes a global [`tracing::Subscriber`] for this [`Cucumber`] with
/// a default [`fmt::Layer`] and [`LevelFilter::INFO`].
///
/// [`fmt::Layer`]: tracing_subscriber::fmt::Layer
#[must_use]
pub fn init_tracing(self) -> Self {
self.configure_and_init_tracing(
format::DefaultFields::new(),
Format::default(),
tracing_subscriber::fmt::layer()
.fmt_fields(format::DefaultFields::new())
.event_format(Format::default()),
|layer| {
tracing_subscriber::registry()
.with(LevelFilter::INFO.and_then(layer))
},
)
}

/// Configures a [`fmt::Layer`], additionally wraps it (for example, into a
/// [`LevelFilter`]), and initializes as a global [`tracing::Subscriber`].
/// Configures the provided [`fmt::Layer`] for this [`Cucumber`] and allows
/// to additionally wrap it (for example, into a [`LevelFilter`]) before
/// initializing it as a global [`tracing::Subscriber`].
///
/// # Example
///
/// ```rust
/// # use cucumber::{Cucumber, World as _};
/// # use tracing_subscriber::{
/// # filter::LevelFilter,
/// # fmt::format::{self, Format},
/// # fmt::{self, format::{self, Format}},
/// # layer::SubscriberExt,
/// # Layer,
/// # };
Expand All @@ -74,11 +76,12 @@ where
/// # let _ = async {
/// World::cucumber()
/// .configure_and_init_tracing(
/// format::DefaultFields::new(),
/// Format::default(),
/// |fmt_layer| {
/// fmt::layer()
/// .fmt_fields(format::DefaultFields::new())
/// .event_format(Format::default()),
/// |layer| {
/// tracing_subscriber::registry()
/// .with(LevelFilter::INFO.and_then(fmt_layer))
/// .with(LevelFilter::INFO.and_then(layer))
/// },
/// )
/// .run_and_exit("./tests/features/doctests.feature")
Expand All @@ -90,8 +93,7 @@ where
#[must_use]
pub fn configure_and_init_tracing<Event, Fields, Sub, Conf, Out>(
self,
fmt_fields: Fields,
event_format: Event,
fmt_layer: tracing_subscriber::fmt::Layer<Sub, Fields, Event>,
configure: Conf,
) -> Self
where
Expand All @@ -118,9 +120,9 @@ where
let (span_close_sender, span_close_receiver) = mpsc::unbounded();

let layer = RecordScenarioId::new(span_close_sender).and_then(
tracing_subscriber::fmt::layer()
.fmt_fields(SkipScenarioIdSpan(fmt_fields))
.event_format(AppendScenarioMsg(event_format))
fmt_layer
.map_fmt_fields(SkipScenarioIdSpan)
.map_event_format(AppendScenarioMsg)
.with_writer(CollectorWriter::new(logs_sender)),
);
Dispatch::new(configure(layer)).init();
Expand Down
10 changes: 7 additions & 3 deletions tests/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ use regex::RegexBuilder;
use tempfile::NamedTempFile;
use tracing_subscriber::{
filter::LevelFilter,
fmt::format::{DefaultFields, Format},
fmt::{
self,
format::{DefaultFields, Format},
},
layer::SubscriberExt as _,
Layer as _,
};
Expand Down Expand Up @@ -48,8 +51,9 @@ async fn test() {
.fail_on_skipped()
.with_default_cli()
.configure_and_init_tracing(
DefaultFields::new(),
Format::default().with_ansi(false).without_time(),
fmt::layer().fmt_fields(DefaultFields::new()).event_format(
Format::default().with_ansi(false).without_time(),
),
|layer| {
tracing_subscriber::registry()
.with(LevelFilter::INFO.and_then(layer))
Expand Down
10 changes: 7 additions & 3 deletions tests/junit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ use regex::RegexBuilder;
use tempfile::NamedTempFile;
use tracing_subscriber::{
filter::LevelFilter,
fmt::format::{DefaultFields, Format},
fmt::{
self,
format::{DefaultFields, Format},
},
layer::SubscriberExt as _,
Layer as _,
};
Expand Down Expand Up @@ -35,8 +38,9 @@ async fn output() {
.fail_on_skipped()
.with_default_cli()
.configure_and_init_tracing(
DefaultFields::new(),
Format::default().with_ansi(false).without_time(),
fmt::layer().fmt_fields(DefaultFields::new()).event_format(
Format::default().with_ansi(false).without_time(),
),
|layer| {
tracing_subscriber::registry()
.with(LevelFilter::INFO.and_then(layer))
Expand Down
10 changes: 7 additions & 3 deletions tests/tracing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ use regex::Regex;
use tokio::{spawn, time};
use tracing_subscriber::{
filter::LevelFilter,
fmt::format::{DefaultFields, Format},
fmt::{
self,
format::{DefaultFields, Format},
},
layer::SubscriberExt as _,
Layer,
};
Expand Down Expand Up @@ -38,8 +41,9 @@ async fn main() {
.fail_on_skipped()
.with_default_cli()
.configure_and_init_tracing(
DefaultFields::new(),
Format::default().with_ansi(false).without_time(),
fmt::layer().fmt_fields(DefaultFields::new()).event_format(
Format::default().with_ansi(false).without_time(),
),
|layer| {
tracing_subscriber::registry()
.with(LevelFilter::INFO.and_then(layer))
Expand Down
Loading