Struct bevy::log::tracing_subscriber::fmt::Layer

pub struct Layer<S, N = DefaultFields, E = Format, W = fn() -> Stdout> { /* private fields */ }
Available on crate features fmt and std only.
Expand description

A Layer that logs formatted representations of tracing events.

§Examples

Constructing a layer with the default configuration:

use tracing_subscriber::{fmt, Registry};
use tracing_subscriber::prelude::*;

let subscriber = Registry::default()
    .with(fmt::Layer::default());

tracing::subscriber::set_global_default(subscriber).unwrap();

Overriding the layer’s behavior:

use tracing_subscriber::{fmt, Registry};
use tracing_subscriber::prelude::*;

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);

Setting a custom event formatter:

use tracing_subscriber::fmt::{self, format, time};
use tracing_subscriber::prelude::*;

let fmt = format().with_timer(time::Uptime::default());
let fmt_layer = fmt::layer()
    .event_format(fmt)
    .with_target(false);

Implementations§

§

impl<S> Layer<S>

pub fn new() -> Layer<S>

Returns a new Layer with the default configuration.

§

impl<S, N, E, W> Layer<S, N, E, W>
where S: Subscriber + for<'a> LookupSpan<'a>, N: for<'writer> FormatFields<'writer> + 'static, W: for<'writer> MakeWriter<'writer> + 'static,

pub fn event_format<E2>(self, e: E2) -> Layer<S, N, E2, W>
where E2: FormatEvent<S, N> + 'static,

Sets the event formatter that the layer being built will use to format events.

The event formatter may be any type implementing the FormatEvent trait, which is implemented for all functions taking a FmtContext, a Writer, and an Event.

§Examples

Setting a type implementing FormatEvent as the formatter:

use tracing_subscriber::fmt::{self, format};

let layer = fmt::layer()
    .event_format(format().compact());

pub fn map_event_format<E2>(self, f: impl FnOnce(E) -> E2) -> Layer<S, N, E2, W>
where E2: FormatEvent<S, N> + 'static,

Updates the event formatter by applying a function to the existing event formatter.

This sets the event formatter that the layer being built will use to record fields.

§Examples

Updating an event formatter:

let layer = tracing_subscriber::fmt::layer()
    .map_event_format(|e| e.compact());
§

impl<S, N, E, W> Layer<S, N, E, W>

pub fn with_writer<W2>(self, make_writer: W2) -> Layer<S, N, E, W2>
where W2: for<'writer> MakeWriter<'writer> + 'static,

Sets the MakeWriter that the layer being built will use to write events.

§Examples

Using stderr rather than stdout:

use std::io;
use tracing_subscriber::fmt;

let layer = fmt::layer()
    .with_writer(io::stderr);

pub fn writer(&self) -> &W

Borrows the writer for this Layer.

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 = fmt::layer().with_writer(non_blocking(std::io::stderr()));
let (layer, reload_handle) = reload::Layer::new(layer);
info!("This will be logged to stderr");
reload_handle.modify(|layer| *layer.writer_mut() = non_blocking(std::io::stdout()));
info!("This will be logged to stdout");

pub fn set_ansi(&mut self, ansi: bool)

Available on crate feature ansi only.

Sets whether this layer should use ANSI terminal formatting escape codes (such as colors).

This method is primarily expected to be used with the reload::Handle::modify method when changing the writer.

pub fn with_test_writer(self) -> Layer<S, N, E, 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 std::io;
use tracing_subscriber::fmt;

let layer = fmt::layer()
    .with_test_writer();

pub fn with_ansi(self, ansi: bool) -> Layer<S, N, E, W>

Sets whether or not the formatter emits ANSI terminal escape codes for colors and other text formatting.

When the “ansi” crate feature flag is enabled, ANSI colors are enabled by default unless the NO_COLOR environment variable is set to a non-empty value. If the NO_COLOR environment variable is set to any non-empty value, then ANSI colors will be suppressed by default. The with_ansi and set_ansi methods can be used to forcibly enable ANSI colors, overriding any NO_COLOR environment variable.

Enabling ANSI escapes (calling with_ansi(true)) requires the “ansi” crate feature flag. Calling with_ansi(true) without the “ansi” feature flag enabled will panic if debug assertions are enabled, or print a warning otherwise.

This method itself is still available without the feature flag. This is to allow ANSI escape codes to be explicitly disabled without having to opt-in to the dependencies required to emit ANSI formatting. This way, code which constructs a formatter that should never emit ANSI escape codes can ensure that they are not used, regardless of whether or not other crates in the dependency graph enable the “ansi” feature flag.

pub fn log_internal_errors(self, log_internal_errors: bool) -> Layer<S, N, E, W>

Sets whether to write errors from FormatEvent to the writer. Defaults to true.

By default, fmt::Layer 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.

pub fn map_writer<W2>(self, f: impl FnOnce(W) -> W2) -> Layer<S, N, E, 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::Level;
use tracing_subscriber::fmt::{self, writer::MakeWriterExt};

let stderr = std::io::stderr.with_max_level(Level::WARN);
let layer = fmt::layer()
    .map_writer(move |w| stderr.or_else(w));
§

impl<S, N, L, T, W> Layer<S, N, Format<L, T>, W>
where N: for<'writer> FormatFields<'writer> + 'static,

pub fn with_timer<T2>(self, timer: T2) -> Layer<S, N, Format<L, T2>, W>

Use the given timer for span and event 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.

pub fn without_time(self) -> Layer<S, N, Format<L, ()>, W>

Do not emit timestamps with spans and event.

pub fn with_span_events(self, kind: FmtSpan) -> Layer<S, N, Format<L, T>, W>

Configures how synthesized events are emitted at points in the span lifecycle.

The following options are available:

  • FmtSpan::NONE: No events will be synthesized when spans are created, entered, exited, or closed. Data from spans will still be included as the context for formatted events. This is the default.
  • FmtSpan::NEW: An event will be synthesized when spans are created.
  • FmtSpan::ENTER: An event will be synthesized when spans are entered.
  • FmtSpan::EXIT: An event will be synthesized when spans are exited.
  • FmtSpan::CLOSE: An event will be synthesized when a span closes. If timestamps are enabled for this formatter, the generated event will contain fields with the span’s busy time (the total time for which it was entered) and idle time (the total time that the span existed but was not entered).
  • FmtSpan::ACTIVE: Events will be synthesized when spans are entered or exited.
  • FmtSpan::FULL: Events will be synthesized whenever a span is created, entered, exited, or closed. If timestamps are enabled, the close event will contain the span’s busy and idle time, as described above.

The options can be enabled in any combination. For instance, the following will synthesize events whenever spans are created and closed:

use tracing_subscriber::fmt;
use tracing_subscriber::fmt::format::FmtSpan;

let subscriber = fmt()
    .with_span_events(FmtSpan::NEW | FmtSpan::CLOSE)
    .finish();

Note that the generated events will only be part of the log output by this formatter; they will not be recorded by other Subscribers or by Layers added to this subscriber.

pub fn with_target(self, display_target: bool) -> Layer<S, N, Format<L, T>, W>

Sets whether or not an event’s target is displayed.

pub fn with_file(self, display_filename: bool) -> Layer<S, N, Format<L, T>, W>

Sets whether or not an event’s source code file path is displayed.

pub fn with_line_number( self, display_line_number: bool ) -> Layer<S, N, Format<L, T>, W>

Sets whether or not an event’s source code line number is displayed.

pub fn with_level(self, display_level: bool) -> Layer<S, N, Format<L, T>, W>

Sets whether or not an event’s level is displayed.

pub fn with_thread_ids( self, display_thread_ids: bool ) -> Layer<S, N, Format<L, T>, W>

Sets whether or not the thread ID of the current thread is displayed when formatting events.

pub fn with_thread_names( self, display_thread_names: bool ) -> Layer<S, N, Format<L, T>, W>

Sets whether or not the name of the current thread is displayed when formatting events.

pub fn compact(self) -> Layer<S, N, Format<Compact, T>, W>
where N: for<'writer> FormatFields<'writer> + 'static,

Sets the layer being built to use a less verbose formatter.

pub fn pretty(self) -> Layer<S, Pretty, Format<Pretty, T>, W>

Available on crate feature ansi only.

Sets the layer being built to use an excessively pretty, human-readable formatter.

§

impl<S, N, E, W> Layer<S, N, E, W>

pub fn fmt_fields<N2>(self, fmt_fields: N2) -> Layer<S, N2, E, W>
where N2: for<'writer> FormatFields<'writer> + 'static,

Sets the field formatter that the layer being built will use to record fields.

pub fn map_fmt_fields<N2>(self, f: impl FnOnce(N) -> N2) -> Layer<S, N2, E, W>
where N2: for<'writer> FormatFields<'writer> + 'static,

Updates the field formatter by applying a function to the existing field formatter.

This sets the field formatter that the layer being built will use to record fields.

§Examples

Updating a field formatter:

use tracing_subscriber::field::MakeExt;
let layer = tracing_subscriber::fmt::layer()
    .map_fmt_fields(|f| f.debug_alt());

Trait Implementations§

§

impl<S, N, E, W> Debug for Layer<S, N, E, W>
where S: Debug, N: Debug, E: Debug, W: Debug,

§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
§

impl<S> Default for Layer<S>

§

fn default() -> Layer<S>

Returns the “default value” for a type. Read more
§

impl<S, N, E, W> Layer<S> for Layer<S, N, E, W>
where S: Subscriber + for<'a> LookupSpan<'a>, N: for<'writer> FormatFields<'writer> + 'static, E: FormatEvent<S, N> + 'static, W: for<'writer> MakeWriter<'writer> + 'static,

§

fn on_new_span(&self, attrs: &Attributes<'_>, id: &Id, ctx: Context<'_, S>)

Notifies this layer that a new span was constructed with the given Attributes and Id.
§

fn on_record(&self, id: &Id, values: &Record<'_>, ctx: Context<'_, S>)

Notifies this layer that a span with the given Id recorded the given values.
§

fn on_enter(&self, id: &Id, ctx: Context<'_, S>)

Notifies this layer that a span with the given ID was entered.
§

fn on_exit(&self, id: &Id, ctx: Context<'_, S>)

Notifies this layer that the span with the given ID was exited.
§

fn on_close(&self, id: Id, ctx: Context<'_, S>)

Notifies this layer that the span with the given ID has been closed.
§

fn on_event(&self, event: &Event<'_>, ctx: Context<'_, S>)

Notifies this layer that an event has occurred.
§

fn on_register_dispatch(&self, subscriber: &Dispatch)

Performs late initialization when installing this layer as a Subscriber. Read more
§

fn on_layer(&mut self, subscriber: &mut S)

Performs late initialization when attaching a Layer to a Subscriber. Read more
§

fn register_callsite(&self, metadata: &'static Metadata<'static>) -> Interest

Registers a new callsite with this layer, returning whether or not the layer is interested in being notified about the callsite, similarly to Subscriber::register_callsite. Read more
§

fn enabled(&self, metadata: &Metadata<'_>, ctx: Context<'_, S>) -> bool

Returns true if this layer is interested in a span or event with the given metadata in the current Context, similarly to Subscriber::enabled. Read more
§

fn on_follows_from(&self, _span: &Id, _follows: &Id, _ctx: Context<'_, S>)

Notifies this layer that a span with the ID span recorded that it follows from the span with the ID follows.
§

fn event_enabled(&self, _event: &Event<'_>, _ctx: Context<'_, S>) -> bool

Called before on_event, to determine if on_event should be called.
§

fn on_id_change(&self, _old: &Id, _new: &Id, _ctx: Context<'_, S>)

Notifies this layer that a span ID has been cloned, and that the subscriber returned a different ID.
§

fn and_then<L>(self, layer: L) -> Layered<L, Self, S>
where L: Layer<S>, Self: Sized,

Composes this layer around the given Layer, returning a Layered struct implementing Layer. Read more
§

fn with_subscriber(self, inner: S) -> Layered<Self, S>
where Self: Sized,

Composes this Layer with the given Subscriber, returning a Layered struct that implements Subscriber. Read more
§

fn with_filter<F>(self, filter: F) -> Filtered<Self, F, S>
where Self: Sized, F: Filter<S>,

Available on crate features registry and std only.
Combines self with a Filter, returning a Filtered layer. Read more

Auto Trait Implementations§

§

impl<S, N, E, W> Freeze for Layer<S, N, E, W>
where W: Freeze, N: Freeze, E: Freeze,

§

impl<S, N, E, W> RefUnwindSafe for Layer<S, N, E, W>

§

impl<S, N, E, W> Send for Layer<S, N, E, W>
where W: Send, N: Send, E: Send,

§

impl<S, N, E, W> Sync for Layer<S, N, E, W>
where W: Sync, N: Sync, E: Sync,

§

impl<S, N, E, W> Unpin for Layer<S, N, E, W>
where W: Unpin, N: Unpin, E: Unpin,

§

impl<S, N, E, W> UnwindSafe for Layer<S, N, E, W>
where W: UnwindSafe, N: UnwindSafe, E: UnwindSafe,

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
§

impl<T, U> AsBindGroupShaderType<U> for T
where U: ShaderType, &'a T: for<'a> Into<U>,

§

fn as_bind_group_shader_type(&self, _images: &RenderAssets<GpuImage>) -> U

Return the T ShaderType for self. When used in AsBindGroup derives, it is safe to assume that all images in self exist.
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

impl<T> Downcast<T> for T

§

fn downcast(&self) -> &T

§

impl<T> Downcast for T
where T: Any,

§

fn into_any(self: Box<T>) -> Box<dyn Any>

Convert Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.
§

fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>

Convert Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be further downcast into Rc<ConcreteType> where ConcreteType implements Trait.
§

fn as_any(&self) -> &(dyn Any + 'static)

Convert &Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &Any’s vtable from &Trait’s.
§

fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)

Convert &mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &mut Any’s vtable from &mut Trait’s.
§

impl<T> DowncastSync for T
where T: Any + Send + Sync,

§

fn into_any_arc(self: Arc<T>) -> Arc<dyn Any + Send + Sync>

Convert Arc<Trait> (where Trait: Downcast) to Arc<Any>. Arc<Any> can then be further downcast into Arc<ConcreteType> where ConcreteType implements Trait.
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<S> FromSample<S> for S

§

fn from_sample_(s: S) -> S

§

impl<T> FromWorld for T
where T: Default,

§

fn from_world(_world: &mut World) -> T

Creates Self using data from the given World.
§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> IntoEither for T

source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
§

impl<F, T> IntoSample<T> for F
where T: FromSample<F>,

§

fn into_sample(self) -> T

§

impl<T> NoneValue for T
where T: Default,

§

type NoneType = T

§

fn null_value() -> T

The none-equivalent value.
§

impl<T> Pointable for T

§

const ALIGN: usize = _

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
source§

impl<R, P> ReadPrimitive<R> for P
where R: Read + ReadEndian<P>, P: Default,

source§

fn read_from_little_endian(read: &mut R) -> Result<Self, Error>

Read this value from the supplied reader. Same as ReadEndian::read_from_little_endian().
source§

fn read_from_big_endian(read: &mut R) -> Result<Self, Error>

Read this value from the supplied reader. Same as ReadEndian::read_from_big_endian().
source§

fn read_from_native_endian(read: &mut R) -> Result<Self, Error>

Read this value from the supplied reader. Same as ReadEndian::read_from_native_endian().
source§

impl<T> Same for T

§

type Output = T

Should always be Self
§

impl<T, U> ToSample<U> for T
where U: FromSample<T>,

§

fn to_sample_(self) -> U

source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<T> Upcast<T> for T

§

fn upcast(&self) -> Option<&T>

§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V

§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
§

impl<T> ConditionalSend for T
where T: Send,

§

impl<S, T> Duplex<S> for T
where T: FromSample<S> + ToSample<S>,

§

impl<T> Settings for T
where T: 'static + Send + Sync,

§

impl<T> WasmNotSend for T
where T: Send,

§

impl<T> WasmNotSendSync for T
where T: WasmNotSend + WasmNotSync,

§

impl<T> WasmNotSync for T
where T: Sync,