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
impl Console
Sourcepub fn new() -> Self
pub fn new() -> Self
Creates a new console target
Examples found in repository?
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
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}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}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}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}Sourcepub fn filtered(self, level: LogLevel) -> Self
pub fn filtered(self, level: LogLevel) -> Self
Builder method to set the custom filter level for this target.
Sourcepub fn output(self, output: Output) -> Self
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?
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}Sourcepub fn filtered_output(self, level: LogLevel, output: Output) -> Self
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?
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 Target for Console
impl Target for Console
Source§fn filter_level(&self) -> Option<LogLevel>
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.