logo
pub struct Layer<S, N = DefaultFields, E = Format<Full>, W = fn() -> Stdout> { /* private fields */ }
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

Returns a new Layer with the default configuration.

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

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

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

Borrows the writer for this Layer.

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

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.

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

Enable ANSI terminal colors for formatted output.

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

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.

Do not emit timestamps with spans and event.

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.

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

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

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

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

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

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

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

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

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

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

Formats the value using the given formatter. Read more

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

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

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

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

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

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

Notifies this layer that an event has occurred.

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

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

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

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

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

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

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

Combines self with a Filter, returning a Filtered layer. Read more

Erases the type of this Layer, returning a Boxed dyn Layer trait object. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

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

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.