Struct json_subscriber::fmt::Layer
source · pub struct Layer<S: for<'lookup> LookupSpan<'lookup> = Registry, W = fn() -> Stdout> { /* private fields */ }Expand description
A Layer that logs JSON formatted representations of tracing events.
This is just a wrapper around JsonLayer which exists for compatibility with
tracing_subscriber.
§Examples
Constructing a layer with the default configuration:
use tracing_subscriber::Registry;
use tracing_subscriber::layer::SubscriberExt as _;
use json_subscriber::fmt;
let subscriber = Registry::default()
.with(fmt::Layer::default());
tracing::subscriber::set_global_default(subscriber).unwrap();Overriding the layer’s behavior:
use tracing_subscriber::Registry;
use tracing_subscriber::layer::SubscriberExt as _;
use json_subscriber::fmt;
let fmt_layer = fmt::layer()
.with_target(false) // don't include event targets when logging
.with_level(false); // don't include event levels when logging
let subscriber = Registry::default().with(fmt_layer);Implementations§
source§impl<S, W> Layer<S, W>where
S: Subscriber + for<'lookup> LookupSpan<'lookup>,
impl<S, W> Layer<S, W>where
S: Subscriber + for<'lookup> LookupSpan<'lookup>,
sourcepub fn with_writer<W2>(self, make_writer: W2) -> Layer<S, W2>where
W2: for<'writer> MakeWriter<'writer> + 'static,
pub fn with_writer<W2>(self, make_writer: W2) -> Layer<S, W2>where
W2: for<'writer> MakeWriter<'writer> + 'static,
Sets the MakeWriter that the JsonLayer being built will use to write events.
§Examples
Using stderr rather than stdout:
let layer = json_subscriber::fmt::layer()
.with_writer(std::io::stderr);sourcepub fn map_writer<W2>(self, f: impl FnOnce(W) -> W2) -> Layer<S, W2>where
W2: for<'writer> MakeWriter<'writer> + 'static,
pub fn map_writer<W2>(self, f: impl FnOnce(W) -> W2) -> Layer<S, W2>where
W2: for<'writer> MakeWriter<'writer> + 'static,
Updates the MakeWriter by applying a function to the existing MakeWriter.
This sets the MakeWriter that the layer being built will use to write events.
§Examples
Redirect output to stderr if level is <= WARN:
use tracing_subscriber::fmt::writer::MakeWriterExt;
let stderr = std::io::stderr.with_max_level(tracing::Level::WARN);
let layer = json_subscriber::fmt::layer()
.map_writer(move |w| stderr.or_else(w));sourcepub fn with_test_writer(self) -> Layer<S, TestWriter>
pub fn with_test_writer(self) -> Layer<S, TestWriter>
Configures the layer to support libtest’s output capturing when used in
unit tests.
See TestWriter for additional details.
§Examples
Using TestWriter to let cargo test capture test output:
use tracing_subscriber::fmt::writer::MakeWriterExt;
let layer = json_subscriber::fmt::layer()
.with_test_writer();sourcepub fn writer_mut(&mut self) -> &mut W
pub fn writer_mut(&mut self) -> &mut W
Mutably borrows the writer for this layer.
This method is primarily expected to be used with the reload::Handle::modify method.
§Examples
let layer = json_subscriber::fmt::layer().with_writer(non_blocking(std::io::stderr()));
let (layer, reload_handle) = reload::Layer::new(layer);
tracing_subscriber::registry().with(layer).init();
info!("This will be logged to stderr");
reload_handle.modify(|subscriber| *subscriber.writer_mut() = non_blocking(std::io::stdout()));
info!("This will be logged to stdout");sourcepub fn inner_layer_mut(&mut self) -> &mut JsonLayer<S, W>
pub fn inner_layer_mut(&mut self) -> &mut JsonLayer<S, W>
Mutably borrows the JsonLayer inside of this layer. This can be useful to add more
information to the output or to change the output with the reload::Handle::modify
method.
§Examples
let mut layer = json_subscriber::layer();
let mut inner = layer.inner_layer_mut();
inner.add_static_field(
"hostname",
serde_json::json!({
"hostname": get_hostname(),
}),
);sourcepub fn log_internal_errors(self, log_internal_errors: bool) -> Self
pub fn log_internal_errors(self, log_internal_errors: bool) -> Self
Sets whether to write errors from FormatEvent to the writer.
Defaults to true.
By default, fmt::JsonLayer will write any FormatEvent-internal errors to the writer.
These errors are unlikely and will only occur if there is a bug in the FormatEvent
implementation or its dependencies.
If writing to the writer fails, the error message is printed to stderr as a fallback.
sourcepub fn flatten_event(self, flatten_event: bool) -> Self
pub fn flatten_event(self, flatten_event: bool) -> Self
Sets the JSON subscriber being built to flatten event metadata.
sourcepub fn with_current_span(self, display_current_span: bool) -> Self
pub fn with_current_span(self, display_current_span: bool) -> Self
Sets whether or not the formatter will include the current span in formatted events.
sourcepub fn with_span_list(self, display_span_list: bool) -> Self
pub fn with_span_list(self, display_span_list: bool) -> Self
Sets whether or not the formatter will include a list (from root to leaf) of all currently entered spans in formatted events.
This overrides any previous calls to with_flat_span_list.
sourcepub fn with_flat_span_list(self, flatten_span_list: bool) -> Self
pub fn with_flat_span_list(self, flatten_span_list: bool) -> Self
Sets whether or not the formatter will include an object containing all parent spans’ fields. If multiple ancestor spans recorded the same field, the span closer to the leaf span overrides the values of spans that are closer to the root spans.
This overrides any previous calls to with_span_list.
sourcepub fn with_timer<T: FormatTime + Send + Sync + 'static>(self, timer: T) -> Self
pub fn with_timer<T: FormatTime + Send + Sync + 'static>(self, timer: T) -> Self
Use the given timer for log message timestamps.
See the time module for the provided timer implementations.
Note that using the "time“” feature flag enables the
additional time formatters UtcTime and LocalTime, which use the
time crate to provide more sophisticated timestamp formatting
options.
sourcepub fn without_time(self) -> Self
pub fn without_time(self) -> Self
Do not emit timestamps with log messages.
sourcepub fn with_target(self, display_target: bool) -> Self
pub fn with_target(self, display_target: bool) -> Self
Sets whether or not an event’s target is displayed.
sourcepub fn with_file(self, display_filename: bool) -> Self
pub fn with_file(self, display_filename: bool) -> Self
Sets whether or not an event’s source code file path is displayed.
sourcepub fn with_line_number(self, display_line_number: bool) -> Self
pub fn with_line_number(self, display_line_number: bool) -> Self
Sets whether or not an event’s source code line number is displayed.
sourcepub fn with_level(self, display_level: bool) -> Self
pub fn with_level(self, display_level: bool) -> Self
Sets whether or not an event’s level is displayed.
sourcepub fn with_thread_names(self, display_thread_name: bool) -> Self
pub fn with_thread_names(self, display_thread_name: bool) -> Self
Sets whether or not the name of the current thread is displayed when formatting events.
sourcepub fn with_thread_ids(self, display_thread_id: bool) -> Self
pub fn with_thread_ids(self, display_thread_id: bool) -> Self
Sets whether or not the thread ID of the current thread is displayed when formatting events.
sourcepub fn with_opentelemetry_ids(self, display_opentelemetry_ids: bool) -> Self
Available on crate feature opentelemetry only.
pub fn with_opentelemetry_ids(self, display_opentelemetry_ids: bool) -> Self
opentelemetry only.Sets whether or not OpenTelemetry trace ID and span ID is displayed when formatting events.
Trait Implementations§
source§impl<S: Subscriber + for<'lookup> LookupSpan<'lookup>> Default for Layer<S>
impl<S: Subscriber + for<'lookup> LookupSpan<'lookup>> Default for Layer<S>
source§impl<S, W> Layer<S> for Layer<S, W>
impl<S, W> Layer<S> for Layer<S, W>
source§fn on_register_dispatch(&self, subscriber: &Dispatch)
fn on_register_dispatch(&self, subscriber: &Dispatch)
Subscriber. Read moresource§fn register_callsite(&self, metadata: &'static Metadata<'static>) -> Interest
fn register_callsite(&self, metadata: &'static Metadata<'static>) -> Interest
Subscriber::register_callsite. Read moresource§fn enabled(&self, metadata: &Metadata<'_>, ctx: Context<'_, S>) -> bool
fn enabled(&self, metadata: &Metadata<'_>, ctx: Context<'_, S>) -> bool
true if this layer is interested in a span or event with the
given metadata in the current Context, similarly to
Subscriber::enabled. Read moresource§fn on_new_span(&self, attrs: &Attributes<'_>, id: &Id, ctx: Context<'_, S>)
fn on_new_span(&self, attrs: &Attributes<'_>, id: &Id, ctx: Context<'_, S>)
Attributes and Id.source§fn on_record(&self, span: &Id, values: &Record<'_>, ctx: Context<'_, S>)
fn on_record(&self, span: &Id, values: &Record<'_>, ctx: Context<'_, S>)
Id recorded the given
values.source§fn on_follows_from(&self, span: &Id, follows: &Id, ctx: Context<'_, S>)
fn on_follows_from(&self, span: &Id, follows: &Id, ctx: Context<'_, S>)
span recorded that it
follows from the span with the ID follows.source§fn on_event(&self, event: &Event<'_>, ctx: Context<'_, S>)
fn on_event(&self, event: &Event<'_>, ctx: Context<'_, S>)
source§fn on_enter(&self, id: &Id, ctx: Context<'_, S>)
fn on_enter(&self, id: &Id, ctx: Context<'_, S>)
source§fn on_exit(&self, id: &Id, ctx: Context<'_, S>)
fn on_exit(&self, id: &Id, ctx: Context<'_, S>)
source§fn on_close(&self, id: Id, ctx: Context<'_, S>)
fn on_close(&self, id: Id, ctx: Context<'_, S>)
source§fn on_id_change(&self, old: &Id, new: &Id, ctx: Context<'_, S>)
fn on_id_change(&self, old: &Id, new: &Id, ctx: Context<'_, S>)
source§fn and_then<L>(self, layer: L) -> Layered<L, Self, S>
fn and_then<L>(self, layer: L) -> Layered<L, Self, S>
Layer, returning a Layered
struct implementing Layer. Read moresource§fn with_subscriber(self, inner: S) -> Layered<Self, S>where
Self: Sized,
fn with_subscriber(self, inner: S) -> Layered<Self, S>where
Self: Sized,
Layer with the given Subscriber, returning a
Layered struct that implements Subscriber. Read more