TracingConfig

Struct TracingConfig 

Source
pub struct TracingConfig {
    pub format: LogFormat,
    pub writer: WriterConfig,
    pub level_config: LevelConfig,
    pub features: FeatureSet,
    pub otel_config: OtelConfig,
    pub global_subscriber: bool,
}
Available on crate feature tracing_subscriber_ext only.
Expand description

Main configuration builder for tracing setup Default create a new tracing configuration with sensible defaults

Fields§

§format: LogFormat

Output format configuration

§writer: WriterConfig

Output destination configuration

§level_config: LevelConfig

Level filtering configuration

§features: FeatureSet

Optional features configuration

§otel_config: OtelConfig

OpenTelemetry configuration

§global_subscriber: bool

Whether to set the subscriber as global default

Implementations§

Source§

impl TracingConfig

Source

pub fn with_format(self, format: LogFormat) -> Self

Set the log format

Source

pub fn with_pretty_format(self) -> Self

Use pretty formatted output (development style)

Source

pub fn with_json_format(self) -> Self

Use JSON formatted output (production style)

Source

pub fn with_full_format(self) -> Self

Use full formatted output

Source

pub fn with_compact_format(self) -> Self

Use compact formatted output

Source

pub fn with_logfmt_format(self) -> Self

Available on crate feature logfmt only.

Use logfmt formatted output (requires ‘logfmt’ feature)

Source

pub fn with_writer(self, writer: WriterConfig) -> Self

Set the output writer

Source

pub fn with_stdout(self) -> Self

Write logs to stdout

Source

pub fn with_stderr(self) -> Self

Write logs to stderr

Source

pub fn with_file<P: AsRef<Path>>(self, path: P) -> Self

Write logs to a file

Source

pub fn with_log_directives(self, directives: impl Into<String>) -> Self

Set log directives (takes precedence over environment variables), for example if you want to set it from cli arguments (verbosity)

Source

pub fn with_default_level(self, level: LevelFilter) -> Self

Set the default log level

Source

pub fn with_env_fallback(self, env_var: impl Into<String>) -> Self

Add an environment variable fallback for log configuration

Source

pub fn with_otel_trace_level(self, level: LevelFilter) -> Self

Set the OpenTelemetry trace level

Source

pub fn with_file_names(self, enabled: bool) -> Self

Enable or disable file names in output

Source

pub fn with_line_numbers(self, enabled: bool) -> Self

Enable or disable line numbers in output

Source

pub fn with_thread_names(self, enabled: bool) -> Self

Enable or disable thread names in output

Source

pub fn with_thread_ids(self, enabled: bool) -> Self

Enable or disable thread IDs in output

Source

pub fn with_span_events(self, events: FmtSpan) -> Self

Configure span event logging

Source

pub fn without_span_events(self) -> Self

Disable span event logging

Source

pub fn with_uptime_timer(self, enabled: bool) -> Self

👎Deprecated: Use TracingConfig::with_timer instead

Enable or disable uptime timer (vs wall clock)

Source

pub fn with_timer(self, timer: LogTimer) -> Self

Configure time logging (wall clock, uptime or none)

Source

pub fn with_target_display(self, enabled: bool) -> Self

Enable or disable target display

Source

pub fn with_otel(self, enabled: bool) -> Self

Enable or disable OpenTelemetry tracing

Source

pub fn with_metrics(self, enabled: bool) -> Self

Enable or disable metrics collection

Source

pub fn with_resource_config(self, config: DetectResource) -> Self

Set resource configuration for OpenTelemetry

Source

pub fn with_global_subscriber(self, global: bool) -> Self

Set whether to initialize the subscriber as global default

When global is true (default), the subscriber is set as the global default. When false, the subscriber is set as thread-local default and the returned Guard must be kept alive to maintain the subscriber.

Source

pub fn build_layer<S>( &self, ) -> Result<Box<dyn Layer<S> + Send + Sync + 'static>, Error>
where S: Subscriber + for<'a> LookupSpan<'a>,

Build a tracing layer with the current configuration

Source

pub fn build_filter_layer(&self) -> Result<EnvFilter, Error>

Build a level filter layer with the current configuration

Source

pub fn init_subscriber(self) -> Result<Guard, Error>

Initialize the tracing subscriber with this configuration

If global_subscriber is true, sets the subscriber as the global default. If false, returns a Guard that maintains the subscriber as the thread-local default.

When OpenTelemetry is disabled, the Guard will contain None for the OtelGuard.

Source

pub fn init_subscriber_ext<F, SOut>(self, transform: F) -> Result<Guard, Error>
where SOut: Subscriber + for<'a> LookupSpan<'a> + Send + Sync, F: FnOnce(Registry) -> SOut,

transform parameter allow to customize the registry/subscriber before the setup of opentelemetry, log, logfilter.

let guard = TracingConfig::default()
   .with_json_format()
   .with_stderr()
   .init_subscriber_ext(|subscriber| subscriber.with(my_layer))?;
Source

pub fn development() -> Self

Configuration preset for development environments

  • Pretty formatting with colors
  • Output to stderr
  • Line numbers and thread names enabled
  • Span events for NEW and CLOSE
  • Full OpenTelemetry integration
Source

pub fn production() -> Self

Configuration preset for production environments

  • JSON formatting for structured logging
  • Output to stdout
  • Minimal metadata (no line numbers or thread names)
  • No span events to reduce verbosity
  • Full OpenTelemetry integration
Source

pub fn debug() -> Self

Configuration preset for debugging

  • Pretty formatting with full verbosity
  • Output to stderr
  • All metadata enabled
  • Full span events
  • Debug level logging
  • Full OpenTelemetry integration
Source

pub fn minimal() -> Self

Configuration preset for minimal logging

  • Compact formatting
  • Output to stdout
  • No metadata or extra features
  • OpenTelemetry disabled for minimal overhead
Source

pub fn testing() -> Self

Configuration preset for testing environments

  • Compact formatting for less noise
  • Output to stderr to separate from test output
  • Basic metadata
  • OpenTelemetry disabled for speed
  • non global registration (of subscriber)

Trait Implementations§

Source§

impl Debug for TracingConfig

Source§

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

Formats the value using the given formatter. Read more
Source§

impl Default for TracingConfig

Source§

fn default() -> Self

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

Auto Trait Implementations§

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
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
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> FutureExt for T

Source§

fn with_context(self, otel_cx: Context) -> WithContext<Self>

Attaches the provided Context to this type, returning a WithContext wrapper. Read more
Source§

fn with_current_context(self) -> WithContext<Self>

Attaches the current Context to this type, returning a WithContext wrapper. Read more
Source§

impl<T> Instrument for T

Source§

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

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

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> IntoRequest<T> for T

Source§

fn into_request(self) -> Request<T>

Wrap the input message T in a tonic::Request
Source§

impl<L> LayerExt<L> for L

Source§

fn named_layer<S>(&self, service: S) -> Layered<<L as Layer<S>>::Service, S>
where L: Layer<S>,

Applies the layer to a service and wraps it in Layered.
Source§

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

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

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

Source§

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>,

Source§

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.
Source§

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

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

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
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

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