A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://docs.rs/tracing-subscriber/latest/tracing_subscriber/filter/targets/struct.Targets.html below:

Targets in tracing_subscriber::filter::targets - Rust

pub struct Targets();

Available on crate features std or alloc only.

Expand description

A filter that enables or disables spans and events based on their target and level.

Targets are typically equal to the Rust module path of the code where the span or event was recorded, although they may be overridden.

This type can be used for both per-layer filtering (using its Filter implementation) and global filtering (using its Layer implementation).

See the documentation on filtering with layers for details.

§Filtering With Targets

A Targets filter consists of one or more target prefixes, paired with LevelFilters. If a span or event’s target begins with one of those prefixes, and its level is at or below the LevelFilter enabled for that prefix, then the span or event will be enabled.

This is similar to the behavior implemented by the env_logger crate in the log ecosystem.

The EnvFilter type also provided by this crate is very similar to Targets, but is capable of a more sophisticated form of filtering where events may also be enabled or disabled based on the span they are recorded in. Targets can be thought of as a lighter-weight form of EnvFilter that can be used instead when this dynamic filtering is not required.

§Examples

A Targets filter can be constructed by programmatically adding targets and levels to enable:

use tracing_subscriber::{filter, prelude::*};
use tracing_core::Level;

let filter = filter::Targets::new()
    .with_target("my_crate", Level::INFO)
    .with_target("my_crate::interesting_module", Level::DEBUG);

tracing_subscriber::registry()
    .with(tracing_subscriber::fmt::layer())
    .with(filter)
    .init();

LevelFilter::OFF can be used to disable a particular target:

use tracing_subscriber::filter::{Targets, LevelFilter};
use tracing_core::Level;

let filter = Targets::new()
    .with_target("my_crate", Level::INFO)
    .with_target("my_crate::annoying_module", LevelFilter::OFF);

Alternatively, Targets implements std::str::FromStr, allowing it to be parsed from a comma-delimited list of target=level pairs. For example:

use tracing_subscriber::filter;
use tracing_core::Level;

let filter = "my_crate=info,my_crate::interesting_module=trace,other_crate=debug"
    .parse::<filter::Targets>()?;

assert_eq!(
    filter,
    filter::Targets::new()
        .with_target("my_crate", Level::INFO)
        .with_target("my_crate::interesting_module", Level::TRACE)
        .with_target("other_crate", Level::DEBUG)
);

This is particularly useful when the list of enabled targets is configurable by the user at runtime.

The Targets filter can be used as a per-layer filter and as a global filter:

use tracing_subscriber::{
    fmt,
    filter::{Targets, LevelFilter},
    prelude::*,
};
use tracing_core::Level;
use std::{sync::Arc, fs::File};

let stdout_log = fmt::layer().pretty();

let file = File::create("debug_log.json")?;
let debug_log = fmt::layer()
    .with_writer(Arc::new(file))
    .json();

tracing_subscriber::registry()
    .with(stdout_log
        .with_filter(
            Targets::default()
                .with_target("my_crate::cool_module", Level::DEBUG)
                .with_default(Level::INFO)
       )
    )
    .with(debug_log)
    .with(
        Targets::default()
            .with_target("my_crate", Level::TRACE)
            .with_target("other_crate", Level::INFO)
            .with_target("other_crate::annoying_module", LevelFilter::OFF)
            .with_target("third_crate", Level::DEBUG)
    ).init();
Source§ Source

Returns a new Targets filter.

This filter will enable no targets. Call with_target or with_targets to add enabled targets, and with_default to change the default level enabled for spans and events that didn’t match any of the provided targets.

Source

Enables spans and events with targets starting with the provided target prefix if they are at or below the provided LevelFilter.

§Examples
use tracing_subscriber::filter;
use tracing_core::Level;

let filter = filter::Targets::new()
    .with_target("my_crate", Level::INFO)
    .with_target("my_crate::interesting_module", Level::DEBUG);

LevelFilter::OFF can be used to disable a particular target:

use tracing_subscriber::filter::{Targets, LevelFilter};
use tracing_core::Level;

let filter = Targets::new()
    .with_target("my_crate", Level::INFO)
    .with_target("my_crate::interesting_module", LevelFilter::OFF);
Source

Adds targets from an iterator of target-LevelFilter pairs to this filter.

§Examples
use tracing_subscriber::filter;
use tracing_core::Level;

let filter = filter::Targets::new()
    .with_targets(vec![
        ("my_crate", Level::INFO),
        ("my_crate::some_module", Level::DEBUG),
        ("my_crate::other_module::cool_stuff", Level::TRACE),
        ("other_crate", Level::WARN)
    ]);

LevelFilter::OFF can be used to disable a particular target:

use tracing_subscriber::filter::{Targets, LevelFilter};
use tracing_core::Level;

let filter = Targets::new()
    .with_target("my_crate", Level::INFO)
    .with_target("my_crate::interesting_module", LevelFilter::OFF);
Source

Sets the default level to enable for spans and events whose targets did not match any of the configured prefixes.

By default, this is LevelFilter::OFF. This means that spans and events will only be enabled if they match one of the configured target prefixes. If this is changed to a different LevelFilter, spans and events with targets that did not match any of the configured prefixes will be enabled if their level is at or below the provided level.

Source

Returns the default level for this filter, if one is set.

The default level is used to filter any spans or events with targets that do not match any of the configured set of prefixes.

The default level can be set for a filter either by using with_default or when parsing from a filter string that includes a level without a target (e.g. "trace").

§Examples
use tracing_subscriber::filter::{LevelFilter, Targets};

let filter = Targets::new().with_default(LevelFilter::INFO);
assert_eq!(filter.default_level(), Some(LevelFilter::INFO));

let filter: Targets = "info".parse().unwrap();
assert_eq!(filter.default_level(), Some(LevelFilter::INFO));

The default level is None if no default is set:

use tracing_subscriber::filter::Targets;

let filter = Targets::new();
assert_eq!(filter.default_level(), None);

let filter: Targets = "my_crate=info".parse().unwrap();
assert_eq!(filter.default_level(), None);

Note that an unset default level (None) behaves like LevelFilter::OFF when the filter is used, but it could also be set explicitly which may be useful to distinguish (such as when merging multiple Targets).

use tracing_subscriber::filter::{LevelFilter, Targets};

let filter = Targets::new().with_default(LevelFilter::OFF);
assert_eq!(filter.default_level(), Some(LevelFilter::OFF));

let filter: Targets = "off".parse().unwrap();
assert_eq!(filter.default_level(), Some(LevelFilter::OFF));
Source

Returns an iterator over the target-LevelFilter pairs in this filter.

The order of iteration is undefined.

§Examples
use tracing_subscriber::filter::{Targets, LevelFilter};
use tracing_core::Level;

let filter = Targets::new()
    .with_target("my_crate", Level::INFO)
    .with_target("my_crate::interesting_module", Level::DEBUG);

let mut targets: Vec<_> = filter.iter().collect();
targets.sort();

assert_eq!(targets, vec![
    ("my_crate", LevelFilter::INFO),
    ("my_crate::interesting_module", LevelFilter::DEBUG),
]);
Source

Returns whether a target-Level pair would be enabled by this Targets.

This method can be used with module_path! from std as the target in order to emulate the behavior of the tracing::event! and tracing::span! macros.

§Examples
use tracing_subscriber::filter::{Targets, LevelFilter};
use tracing_core::Level;

let filter = Targets::new()
    .with_target("my_crate", Level::INFO)
    .with_target("my_crate::interesting_module", Level::DEBUG);

assert!(filter.would_enable("my_crate", &Level::INFO));
assert!(!filter.would_enable("my_crate::interesting_module", &Level::TRACE));
Source§ Source§ Source§ Source§ Source§ Source§

Extends a collection with the contents of an iterator.

Read more Source§ 🔬This is a nightly-only experimental API. (extend_one #72631)

Extends a collection with exactly one element.

Source§ 🔬This is a nightly-only experimental API. (extend_one #72631)

Reserves capacity in a collection for the given number of additional elements.

Read more Source§

Available on crate feature registry only.

Source§

Available on crate feature std only.

Source§

Available on crate feature std only.

Source§

Available on crate feature std only.

Source§

Available on crate feature std only.

Called before the filtered [

Layer]'s [

on_event

], to determine if

on_event` should be called.

Read more Source§

Available on crate feature std only.

Notifies this filter that a new span was constructed with the given

Attributes

and

Id

.

Read more Source§

Available on crate feature std only.

Notifies this filter that a span with the given

Id

recorded the given

values

.

Read more Source§

Available on crate feature std only.

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

Read more Source§

Available on crate feature std only.

Notifies this filter that a span with the given ID was exited.

Read more Source§

Available on crate feature std only.

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

Read more Source§ Source§ Source§

The associated error which can be returned from parsing.

Source§

Parses a string

s

to return a value of this type.

Read more Source§ Source§

The type of the elements being iterated over.

Source§

Which kind of iterator are we turning this into?

Source§ Source§ Source§

The type of the elements being iterated over.

Source§

Which kind of iterator are we turning this into?

Source§ Source§ Source§ Source§ Source§ Source§ Source§

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

Source§

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

Source§

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

Source§ Source§

Notifies this layer that an event has occurred.

Source§

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

Source§

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

Source§

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

Source§

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

Source§

Composes this layer around the given

Layer

, returning a

Layered

struct implementing

Layer

.

Read more Source§ Source§

Available on crate features registry and std only.

Source§

Available on crate features alloc or std only.

Source§ Source§

Tests for self and other values to be equal, and is used by ==.

1.0.0 · Source§

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.

Source§ Source§ Source§ Source§ Source§ Source§ 🔬This is a nightly-only experimental API. (clone_to_uninit #126799)

Performs copy-assignment from

self

to

dest

.

Read more Source§ Source§

Available on crate features registry and std only.

Combines this

Filter

with another

Filter

s so that spans and events are enabled if and only if

both

filters return

true

.

Read more Source§

Available on crate features registry and std only.

Combines two

Filter

s so that spans and events are enabled if

either

filter returns

true

.

Read more Source§

Available on crate features registry and std only.

Inverts

self

, returning a filter that enables spans and events only if

self

would

not

enable them.

Read more Source§

Available on crate features registry and std only.

Source§ Source§

Returns the argument unchanged.

Source§ Source§ Source§

Calls U::from(self).

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

Source§ Source§

The resulting type after obtaining ownership.

Source§

Creates owned data from borrowed data, usually by cloning.

Read more Source§

Uses borrowed data to replace owned data, usually by cloning.

Read more Source§ Source§ Source§

The type returned in the event of a conversion error.

Source§

Performs the conversion.

Source§ Source§

The type returned in the event of a conversion error.

Source§

Performs the conversion.

Source§

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4