Struct DispatchBuilder

Source
pub struct DispatchBuilder<const APPEND: bool> { /* private fields */ }
Expand description

A builder for configuring a log dispatch, including filters and appenders.

§Examples

use logforth::append;

logforth::builder()
    .dispatch(|d| {
        d.filter(log::LevelFilter::Info)
            .append(append::Stdout::default())
    })
    .apply();

Implementations§

Source§

impl DispatchBuilder<false>

Source

pub fn filter(self, filter: impl Into<Box<dyn Filter>>) -> Self

Add a filter to this dispatch.

§Examples
use logforth::append;

logforth::builder()
    .dispatch(|d| {
        d.filter(log::LevelFilter::Error)
            .append(append::Stderr::default())
    })
    .apply();
Examples found in repository?
examples/custom_layout_filter.rs (line 48)
45fn main() {
46    logforth::builder()
47        .dispatch(|d| {
48            d.filter(CustomFilter)
49                .append(append::Stdout::default().with_layout(CustomLayout))
50        })
51        .apply();
52
53    log::error!("Hello error!");
54    log::warn!("Hello warn!");
55    log::info!("Hello info!");
56    log::debug!("Hello debug!");
57    log::trace!("Hello trace!");
58}
More examples
Hide additional examples
examples/multiple_dispatches.rs (line 21)
18fn main() {
19    logforth::builder()
20        .dispatch(|d| {
21            d.filter(LevelFilter::Error)
22                .append(append::Stderr::default())
23        })
24        .dispatch(|d| {
25            d.filter(LevelFilter::Info)
26                .append(append::Stdout::default())
27        })
28        .apply();
29
30    log::error!("Hello error!");
31    log::warn!("Hello warn!");
32    log::info!("Hello info!");
33    log::debug!("Hello debug!");
34    log::trace!("Hello trace!");
35}
examples/syslog.rs (line 21)
17fn main() {
18    let (append, _guard) = SyslogBuilder::tcp_well_known().unwrap().build();
19
20    logforth::builder()
21        .dispatch(|d| d.filter(log::LevelFilter::Trace).append(append))
22        .apply();
23
24    let repeat = 1;
25
26    for i in 0..repeat {
27        log::error!("Hello syslog error!");
28        log::warn!("Hello syslog warn!");
29        log::info!("Hello syslog info!");
30        log::debug!("Hello syslog debug!");
31        log::trace!("Hello syslog trace!");
32
33        if i + 1 < repeat {
34            std::thread::sleep(std::time::Duration::from_secs(10));
35        }
36    }
37}
examples/single_file.rs (line 25)
18fn main() {
19    let (single_writer, _guard) = SingleFileBuilder::new("my.log")
20        .layout(JsonLayout::default())
21        .build()
22        .unwrap();
23
24    logforth::builder()
25        .dispatch(|d| d.filter(log::LevelFilter::Trace).append(single_writer))
26        .apply();
27
28    let repeat = 1;
29
30    for i in 0..repeat {
31        log::error!("Hello error!");
32        log::warn!("Hello warn!");
33        log::info!("Hello info!");
34        log::debug!("Hello debug!");
35        log::trace!("Hello trace!");
36
37        if i + 1 < repeat {
38            std::thread::sleep(std::time::Duration::from_secs(10));
39        }
40    }
41}
examples/rolling_file.rs (line 28)
19fn main() {
20    let (rolling_writer, _guard) = RollingFileBuilder::new("logs")
21        .layout(JsonLayout::default())
22        .rotation(Rotation::Daily)
23        .filename_prefix("app_log")
24        .build()
25        .unwrap();
26
27    logforth::builder()
28        .dispatch(|d| d.filter(log::LevelFilter::Trace).append(rolling_writer))
29        .apply();
30
31    let repeat = 1;
32
33    for i in 0..repeat {
34        log::error!("Hello error!");
35        log::warn!("Hello warn!");
36        log::info!("Hello info!");
37        log::debug!("Hello debug!");
38        log::trace!("Hello trace!");
39
40        if i + 1 < repeat {
41            std::thread::sleep(std::time::Duration::from_secs(10));
42        }
43    }
44}
Source

pub fn diagnostic(self, diagnostic: impl Into<Box<dyn Diagnostic>>) -> Self

Add a diagnostic to this dispatch.

§Examples
use logforth::append;
use logforth::diagnostic;

logforth::builder()
    .dispatch(|d| {
        d.filter(log::LevelFilter::Error)
            .diagnostic(diagnostic::ThreadLocalDiagnostic::default())
            .append(append::Stderr::default())
    })
    .apply();
Source§

impl<const APPEND: bool> DispatchBuilder<APPEND>

Source

pub fn append(self, append: impl Into<Box<dyn Append>>) -> DispatchBuilder<true>

Add an appender to this dispatch.

§Examples
use logforth::append;

logforth::builder()
    .dispatch(|d| d.append(append::Stdout::default()))
    .apply();
Examples found in repository?
examples/json_stdout.rs (line 20)
18fn main() {
19    logforth::builder()
20        .dispatch(|d| d.append(append::Stdout::default().with_layout(JsonLayout::default())))
21        .apply();
22
23    log::info!("This is an info message.");
24    log::debug!("This debug message will not be printed by default.");
25}
More examples
Hide additional examples
examples/custom_layout_filter.rs (line 49)
45fn main() {
46    logforth::builder()
47        .dispatch(|d| {
48            d.filter(CustomFilter)
49                .append(append::Stdout::default().with_layout(CustomLayout))
50        })
51        .apply();
52
53    log::error!("Hello error!");
54    log::warn!("Hello warn!");
55    log::info!("Hello info!");
56    log::debug!("Hello debug!");
57    log::trace!("Hello trace!");
58}
examples/journald.rs (line 20)
16fn main() {
17    use logforth::append::Journald;
18
19    let append = Journald::new().unwrap();
20    logforth::builder().dispatch(|d| d.append(append)).apply();
21
22    log::error!("Hello, journald at ERROR!");
23    log::warn!("Hello, journald at WARN!");
24    log::info!("Hello, journald at INFO!");
25    log::debug!("Hello, journald at DEBUG!");
26    log::trace!("Hello, journald at TRACE!");
27}
examples/multiple_dispatches.rs (line 22)
18fn main() {
19    logforth::builder()
20        .dispatch(|d| {
21            d.filter(LevelFilter::Error)
22                .append(append::Stderr::default())
23        })
24        .dispatch(|d| {
25            d.filter(LevelFilter::Info)
26                .append(append::Stdout::default())
27        })
28        .apply();
29
30    log::error!("Hello error!");
31    log::warn!("Hello warn!");
32    log::info!("Hello info!");
33    log::debug!("Hello debug!");
34    log::trace!("Hello trace!");
35}
examples/syslog.rs (line 21)
17fn main() {
18    let (append, _guard) = SyslogBuilder::tcp_well_known().unwrap().build();
19
20    logforth::builder()
21        .dispatch(|d| d.filter(log::LevelFilter::Trace).append(append))
22        .apply();
23
24    let repeat = 1;
25
26    for i in 0..repeat {
27        log::error!("Hello syslog error!");
28        log::warn!("Hello syslog warn!");
29        log::info!("Hello syslog info!");
30        log::debug!("Hello syslog debug!");
31        log::trace!("Hello syslog trace!");
32
33        if i + 1 < repeat {
34            std::thread::sleep(std::time::Duration::from_secs(10));
35        }
36    }
37}
examples/single_file.rs (line 25)
18fn main() {
19    let (single_writer, _guard) = SingleFileBuilder::new("my.log")
20        .layout(JsonLayout::default())
21        .build()
22        .unwrap();
23
24    logforth::builder()
25        .dispatch(|d| d.filter(log::LevelFilter::Trace).append(single_writer))
26        .apply();
27
28    let repeat = 1;
29
30    for i in 0..repeat {
31        log::error!("Hello error!");
32        log::warn!("Hello warn!");
33        log::info!("Hello info!");
34        log::debug!("Hello debug!");
35        log::trace!("Hello trace!");
36
37        if i + 1 < repeat {
38            std::thread::sleep(std::time::Duration::from_secs(10));
39        }
40    }
41}

Trait Implementations§

Source§

impl<const APPEND: bool> Debug for DispatchBuilder<APPEND>

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<const APPEND: bool> Freeze for DispatchBuilder<APPEND>

§

impl<const APPEND: bool> !RefUnwindSafe for DispatchBuilder<APPEND>

§

impl<const APPEND: bool> Send for DispatchBuilder<APPEND>

§

impl<const APPEND: bool> Sync for DispatchBuilder<APPEND>

§

impl<const APPEND: bool> Unpin for DispatchBuilder<APPEND>

§

impl<const APPEND: bool> !UnwindSafe for DispatchBuilder<APPEND>

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

impl<T> ErasedDestructor for T
where T: 'static,