Struct Console

Source
pub struct Console { /* private fields */ }
Expand description

Standard console output target.

This target writes log messages to the standard output (stdout) or standard error (stderr) using the Rust println! | eprintln! macro.

Implementations§

Source§

impl Console

Source

pub fn new() -> Self

Creates a new console target

Examples found in repository?
examples/output.rs (line 6)
3fn main() {
4    traccia::init_with_config(traccia::Config {
5        level: LogLevel::Debug,
6        targets: vec![Box::new(Console::new().output(Output::Stderr))],
7        ..Default::default()
8    });
9
10    debug!("This will be logged to stderr.");
11    info!("In fact, all messages will be logged to stderr.");
12    error!("This is an error logged to stderr!!!");
13    fatal!("This is a fatal error logged to stderr!!!");
14}
More examples
Hide additional examples
examples/filtered-output.rs (line 7)
3fn main() {
4    traccia::init_with_config(traccia::Config {
5        level: LogLevel::Debug,
6        targets: vec![Box::new(
7            Console::new()
8                .filtered_output(LogLevel::Error, Output::Stderr)
9                .filtered_output(LogLevel::Fatal, Output::Stderr),
10        )],
11        ..Default::default()
12    });
13
14    debug!("This will be logged to stdout");
15    info!("In fact, only error and fatal messages will be logged to stderr.");
16
17    error!("This is an error logged to stderr!!!");
18    fatal!("This is a fatal error logged to stderr!!!");
19}
examples/level-filtering.rs (line 7)
3fn main() {
4    traccia::init_with_config(traccia::Config {
5        level: LogLevel::Trace,
6        targets: vec![
7            Box::new(traccia::Console::new()),
8            Box::new(
9                traccia::File::new("./.logs/latest.log", FileMode::Truncate)
10                    .expect("Failed to open file.")
11                    .filtered(LogLevel::Fatal),
12            ),
13        ],
14        ..Default::default()
15    });
16
17    info!("This will not be written to latest.log, but will be printed to console.");
18    error!("It will write fatal messages only!");
19    fatal!("Like this :(");
20}
examples/target.rs (line 7)
3fn main() {
4    traccia::init_with_config(traccia::Config {
5        level: LogLevel::Trace,
6        targets: vec![
7            Box::new(traccia::Console::new()),
8            Box::new(
9                traccia::File::new(".logs/latest.log", FileMode::Truncate)
10                    .unwrap()
11                    .filtered(LogLevel::Fatal),
12            ),
13        ],
14        ..Default::default()
15    });
16
17    trace!("This is a trace message");
18    debug!("This is a debug message");
19    info!("This is an info message");
20    warn!("This is a warn message");
21    error!("This is an error message");
22    fatal!("This is a fatal message");
23}
examples/hooks.rs (line 27)
3fn main() {
4    traccia::set_hook(Hook::AfterLog(Box::new(|_, target| {
5        if let TargetId::Console(_) = target {
6            println!("This will be printed after the log message");
7        }
8    })));
9
10    traccia::set_hook(Hook::BeforeLog(Box::new(|level, target| {
11        if let TargetId::File(_) = target {
12            if level == LogLevel::Info {
13                println!("This will be printed only before calling the info! macro on a file.")
14            }
15        }
16    })));
17
18    traccia::set_hook(Hook::BeforeLog(Box::new(|_, target| {
19        if let TargetId::Console(_) = target {
20            println!("This will be printed before the log message");
21        }
22    })));
23
24    traccia::init_with_config(traccia::Config {
25        level: LogLevel::Trace,
26        targets: vec![
27            Box::new(traccia::Console::new()),
28            Box::new(
29                traccia::File::new("./.logs/latest.log", traccia::FileMode::Truncate)
30                    .expect("Failed to open file."),
31            ),
32        ],
33        ..Default::default()
34    });
35
36    info!("This is a test log message");
37    warn!("This is a test warning message");
38}
Source

pub fn filtered(self, level: LogLevel) -> Self

Builder method to set the custom filter level for this target.

Source

pub fn output(self, output: Output) -> Self

Builder method to set the custom output for the console. This will write to the output for all the logs that target this console.

If you want to set the output for a specific log level, use filtered_outputs.

(e.g. You want to write all LogLevel::Error logs to stderr and all other logs to stdout)

Examples found in repository?
examples/output.rs (line 6)
3fn main() {
4    traccia::init_with_config(traccia::Config {
5        level: LogLevel::Debug,
6        targets: vec![Box::new(Console::new().output(Output::Stderr))],
7        ..Default::default()
8    });
9
10    debug!("This will be logged to stderr.");
11    info!("In fact, all messages will be logged to stderr.");
12    error!("This is an error logged to stderr!!!");
13    fatal!("This is a fatal error logged to stderr!!!");
14}
Source

pub fn filtered_output(self, level: LogLevel, output: Output) -> Self

Builder method to set the custom output for a specific log level. This behaves like the output method, but only applies to the specified log level.

If you want to set the output for all logs, use output. Note: This will override the output set by output.

(e.g. If you set output to Stderr, calling filtered_outputs(LogLevel::Info, Output::Stdout) will log all LogLevel::Info logs to stdout and all other logs to stderr)

Examples found in repository?
examples/filtered-output.rs (line 8)
3fn main() {
4    traccia::init_with_config(traccia::Config {
5        level: LogLevel::Debug,
6        targets: vec![Box::new(
7            Console::new()
8                .filtered_output(LogLevel::Error, Output::Stderr)
9                .filtered_output(LogLevel::Fatal, Output::Stderr),
10        )],
11        ..Default::default()
12    });
13
14    debug!("This will be logged to stdout");
15    info!("In fact, only error and fatal messages will be logged to stderr.");
16
17    error!("This is an error logged to stderr!!!");
18    fatal!("This is a fatal error logged to stderr!!!");
19}

Trait Implementations§

Source§

impl Clone for Console

Source§

fn clone(&self) -> Console

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Target for Console

Source§

fn write(&self, level: LogLevel, formatted: &str) -> Result<(), Error>

Writes the formatted log message to the console.

§Arguments
  • formatted - The formatted log message to write
§Returns

Always returns Ok(())

Source§

fn filter_level(&self) -> Option<LogLevel>

Returns the custom filter level for the console target. If the filter level is set, log messages with a lower level will be ignored.

Useful for filtering log messages written to the console.

Source§

fn id(&self) -> TargetId

Returns the target ID for the console target. This is used to identify the target in the logger.

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. 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.