Struct FileLogger

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

Implements a GenLogger that outputs to File

§Simple Example

Instantiates a logger with default message and timestamp format and log file written in ‘append’ mode (as opposed to truncating first).

use log::info;
use log::LevelFilter;
use poly_logger::FileLogger;

// Currently it is mandatory to set the filename, but I may 
// introduce a default where an auto-named file is created
// in the current directory
let mut logger = FileLogger::new(LevelFilter::Info);
logger.filename("./test.log");  
logger.init().unwrap();
info!("This is an INFO message");

§Customized Example

Instantiates a logger with custom timestamp and message format. We are also setting the truncate flag so that log file is overwritten rather than appended to.

use log::info;
use log::LevelFilter;
use poly_logger::FileLogger;

let mut logger = FileLogger::new(LevelFilter::Info);
logger.timestamp_format("%F %X%.3f %Z")
      .msg_format("{level} [{timestamp} {file}:{line}] - {args}")
      .truncate(false)
      .filename("./test.log");
logger.init().unwrap();
info!("This is an INFO message with custom formatting");

§Note

The call to new() is actually returning an instance of GenLogger. The StderrLogger struct is just a way to instantiate a GenLogger with Stderr as the target for output. See GenLogger for the full list of methods that can be called on the logger instance.

Implementations§

Source§

impl FileLogger

Source

pub fn new(level_filter: LevelFilter) -> FileLogger

Unlike the StderrLogger or StdoutLogger we return a FileLogger struct instead of a GenLogger struct as we need to specify file-specific options after creation. A call to create() returns the GenLogger instance we need, though this is typically done by calling init().

Examples found in repository?
examples/doc_poly.rs (line 8)
4fn main() {
5    let mut stderr_log = StderrLogger::new(LevelFilter::Debug);
6    stderr_log.msg_format("{args}");
7
8    let mut file_log = FileLogger::new(LevelFilter::Info);
9    file_log.filename("./test.log");
10
11    let mut poly_log = PolyLogger::new();
12    poly_log.add(file_log.create()); // create() returns the GenLogger
13    poly_log.add(stderr_log);
14    poly_log.init().unwrap();
15
16    info!("This goes to both loggers");
17    debug!("This only goes to stderr");
18}
More examples
Hide additional examples
examples/file.rs (line 6)
5fn main() {
6    let mut logger = FileLogger::new(LevelFilter::Info);
7    let filename = "/tmp/test.log";
8    println!("Logging to {}", filename);
9    logger.timestamp_format("%F %X%.3f %Z")
10          .msg_format("{level} [{timestamp} {file}:{line}] - {args}")
11          .truncate(false)
12          .filename(filename);
13    logger.init().unwrap();
14
15    trace!("This is an TRACE message");
16    debug!("This is a DEBUG message");
17    info!("This is an INFO message");
18    warn!("This is a WARN message");
19    error!("This is an ERROR message");
20}
examples/poly.rs (line 34)
5fn main() {
6    // Create some logger instances and run all through
7    // the PolyLogger
8    
9    // Default format 
10    let tl0 = StderrLogger::new(LevelFilter::Info);
11
12    // Custom format
13    let mut tl1 = StderrLogger::new(LevelFilter::Warn);
14    tl1.timestamp_format("%a %b %e %T %Y")
15       .msg_format("Custom: [{timestamp}] {level} [{file}:{line}] - {args}");
16
17    // Simpler format
18    let mut tl2 = StderrLogger::new(LevelFilter::Info);
19    tl2.msg_format("Simple1: {level} [{timestamp}] {args}")
20       .timestamp_format("%T");
21
22    // Even simpler
23    let mut tl3 = StderrLogger::new(LevelFilter::Debug);
24    tl3.msg_format("Simple2: {level} - {args}")
25       .timestamp_format("");
26
27    // Raw format to stdout
28    let mut tl4 = StdoutLogger::new(LevelFilter::Trace);
29    tl4.msg_format("{args}")
30       .timestamp_format("");
31
32    // File logger
33    let filename = "/tmp/file_logger.log";
34    let mut fl0 = FileLogger::new(LevelFilter::Info);
35    fl0.msg_format("Simple1: {level} [{timestamp}] {args}")
36       .timestamp_format("%T")
37       .filename(filename);
38    println!("Logging to {}", filename);
39    
40    // Create the poly logger and add our logger instances
41    let mut pl = PolyLogger::new();
42    pl.add(tl0);
43    pl.add(tl1);
44    pl.add(tl2);
45    pl.add(tl3);
46    pl.add(tl4);
47    pl.add(fl0.create());
48    pl.init().unwrap();
49
50    trace!("This is an TRACE message");
51    eprintln!("------------------------------");
52    debug!("This is a DEBUG message");
53    eprintln!("------------------------------");
54    info!("This is an INFO message");
55    eprintln!("------------------------------");
56    warn!("This is a WARN message");
57    eprintln!("------------------------------");
58    error!("This is an ERROR message");
59}
Source

pub fn init(&self) -> Result<(), SetLoggerError>

Calls create() to get the GenLogger instance which is then in turn initialized.

Examples found in repository?
examples/file.rs (line 13)
5fn main() {
6    let mut logger = FileLogger::new(LevelFilter::Info);
7    let filename = "/tmp/test.log";
8    println!("Logging to {}", filename);
9    logger.timestamp_format("%F %X%.3f %Z")
10          .msg_format("{level} [{timestamp} {file}:{line}] - {args}")
11          .truncate(false)
12          .filename(filename);
13    logger.init().unwrap();
14
15    trace!("This is an TRACE message");
16    debug!("This is a DEBUG message");
17    info!("This is an INFO message");
18    warn!("This is a WARN message");
19    error!("This is an ERROR message");
20}
Source

pub fn timestamp_format(&mut self, format: &'static str) -> &mut Self

Sets timestamp format for the underlying GenLogger instance

Examples found in repository?
examples/file.rs (line 9)
5fn main() {
6    let mut logger = FileLogger::new(LevelFilter::Info);
7    let filename = "/tmp/test.log";
8    println!("Logging to {}", filename);
9    logger.timestamp_format("%F %X%.3f %Z")
10          .msg_format("{level} [{timestamp} {file}:{line}] - {args}")
11          .truncate(false)
12          .filename(filename);
13    logger.init().unwrap();
14
15    trace!("This is an TRACE message");
16    debug!("This is a DEBUG message");
17    info!("This is an INFO message");
18    warn!("This is a WARN message");
19    error!("This is an ERROR message");
20}
More examples
Hide additional examples
examples/poly.rs (line 36)
5fn main() {
6    // Create some logger instances and run all through
7    // the PolyLogger
8    
9    // Default format 
10    let tl0 = StderrLogger::new(LevelFilter::Info);
11
12    // Custom format
13    let mut tl1 = StderrLogger::new(LevelFilter::Warn);
14    tl1.timestamp_format("%a %b %e %T %Y")
15       .msg_format("Custom: [{timestamp}] {level} [{file}:{line}] - {args}");
16
17    // Simpler format
18    let mut tl2 = StderrLogger::new(LevelFilter::Info);
19    tl2.msg_format("Simple1: {level} [{timestamp}] {args}")
20       .timestamp_format("%T");
21
22    // Even simpler
23    let mut tl3 = StderrLogger::new(LevelFilter::Debug);
24    tl3.msg_format("Simple2: {level} - {args}")
25       .timestamp_format("");
26
27    // Raw format to stdout
28    let mut tl4 = StdoutLogger::new(LevelFilter::Trace);
29    tl4.msg_format("{args}")
30       .timestamp_format("");
31
32    // File logger
33    let filename = "/tmp/file_logger.log";
34    let mut fl0 = FileLogger::new(LevelFilter::Info);
35    fl0.msg_format("Simple1: {level} [{timestamp}] {args}")
36       .timestamp_format("%T")
37       .filename(filename);
38    println!("Logging to {}", filename);
39    
40    // Create the poly logger and add our logger instances
41    let mut pl = PolyLogger::new();
42    pl.add(tl0);
43    pl.add(tl1);
44    pl.add(tl2);
45    pl.add(tl3);
46    pl.add(tl4);
47    pl.add(fl0.create());
48    pl.init().unwrap();
49
50    trace!("This is an TRACE message");
51    eprintln!("------------------------------");
52    debug!("This is a DEBUG message");
53    eprintln!("------------------------------");
54    info!("This is an INFO message");
55    eprintln!("------------------------------");
56    warn!("This is a WARN message");
57    eprintln!("------------------------------");
58    error!("This is an ERROR message");
59}
Source

pub fn msg_format(&mut self, format: &'static str) -> &mut Self

Sets message format for the underlying GenLogger instance

Examples found in repository?
examples/file.rs (line 10)
5fn main() {
6    let mut logger = FileLogger::new(LevelFilter::Info);
7    let filename = "/tmp/test.log";
8    println!("Logging to {}", filename);
9    logger.timestamp_format("%F %X%.3f %Z")
10          .msg_format("{level} [{timestamp} {file}:{line}] - {args}")
11          .truncate(false)
12          .filename(filename);
13    logger.init().unwrap();
14
15    trace!("This is an TRACE message");
16    debug!("This is a DEBUG message");
17    info!("This is an INFO message");
18    warn!("This is a WARN message");
19    error!("This is an ERROR message");
20}
More examples
Hide additional examples
examples/poly.rs (line 35)
5fn main() {
6    // Create some logger instances and run all through
7    // the PolyLogger
8    
9    // Default format 
10    let tl0 = StderrLogger::new(LevelFilter::Info);
11
12    // Custom format
13    let mut tl1 = StderrLogger::new(LevelFilter::Warn);
14    tl1.timestamp_format("%a %b %e %T %Y")
15       .msg_format("Custom: [{timestamp}] {level} [{file}:{line}] - {args}");
16
17    // Simpler format
18    let mut tl2 = StderrLogger::new(LevelFilter::Info);
19    tl2.msg_format("Simple1: {level} [{timestamp}] {args}")
20       .timestamp_format("%T");
21
22    // Even simpler
23    let mut tl3 = StderrLogger::new(LevelFilter::Debug);
24    tl3.msg_format("Simple2: {level} - {args}")
25       .timestamp_format("");
26
27    // Raw format to stdout
28    let mut tl4 = StdoutLogger::new(LevelFilter::Trace);
29    tl4.msg_format("{args}")
30       .timestamp_format("");
31
32    // File logger
33    let filename = "/tmp/file_logger.log";
34    let mut fl0 = FileLogger::new(LevelFilter::Info);
35    fl0.msg_format("Simple1: {level} [{timestamp}] {args}")
36       .timestamp_format("%T")
37       .filename(filename);
38    println!("Logging to {}", filename);
39    
40    // Create the poly logger and add our logger instances
41    let mut pl = PolyLogger::new();
42    pl.add(tl0);
43    pl.add(tl1);
44    pl.add(tl2);
45    pl.add(tl3);
46    pl.add(tl4);
47    pl.add(fl0.create());
48    pl.init().unwrap();
49
50    trace!("This is an TRACE message");
51    eprintln!("------------------------------");
52    debug!("This is a DEBUG message");
53    eprintln!("------------------------------");
54    info!("This is an INFO message");
55    eprintln!("------------------------------");
56    warn!("This is a WARN message");
57    eprintln!("------------------------------");
58    error!("This is an ERROR message");
59}
Source

pub fn truncate(&mut self, truncate: bool) -> &mut Self

Truncates log file before writing. Default is to append

Examples found in repository?
examples/file.rs (line 11)
5fn main() {
6    let mut logger = FileLogger::new(LevelFilter::Info);
7    let filename = "/tmp/test.log";
8    println!("Logging to {}", filename);
9    logger.timestamp_format("%F %X%.3f %Z")
10          .msg_format("{level} [{timestamp} {file}:{line}] - {args}")
11          .truncate(false)
12          .filename(filename);
13    logger.init().unwrap();
14
15    trace!("This is an TRACE message");
16    debug!("This is a DEBUG message");
17    info!("This is an INFO message");
18    warn!("This is a WARN message");
19    error!("This is an ERROR message");
20}
Source

pub fn filename(&mut self, filename: &'static str) -> &mut Self

Sets log file name

Examples found in repository?
examples/doc_poly.rs (line 9)
4fn main() {
5    let mut stderr_log = StderrLogger::new(LevelFilter::Debug);
6    stderr_log.msg_format("{args}");
7
8    let mut file_log = FileLogger::new(LevelFilter::Info);
9    file_log.filename("./test.log");
10
11    let mut poly_log = PolyLogger::new();
12    poly_log.add(file_log.create()); // create() returns the GenLogger
13    poly_log.add(stderr_log);
14    poly_log.init().unwrap();
15
16    info!("This goes to both loggers");
17    debug!("This only goes to stderr");
18}
More examples
Hide additional examples
examples/file.rs (line 12)
5fn main() {
6    let mut logger = FileLogger::new(LevelFilter::Info);
7    let filename = "/tmp/test.log";
8    println!("Logging to {}", filename);
9    logger.timestamp_format("%F %X%.3f %Z")
10          .msg_format("{level} [{timestamp} {file}:{line}] - {args}")
11          .truncate(false)
12          .filename(filename);
13    logger.init().unwrap();
14
15    trace!("This is an TRACE message");
16    debug!("This is a DEBUG message");
17    info!("This is an INFO message");
18    warn!("This is a WARN message");
19    error!("This is an ERROR message");
20}
examples/poly.rs (line 37)
5fn main() {
6    // Create some logger instances and run all through
7    // the PolyLogger
8    
9    // Default format 
10    let tl0 = StderrLogger::new(LevelFilter::Info);
11
12    // Custom format
13    let mut tl1 = StderrLogger::new(LevelFilter::Warn);
14    tl1.timestamp_format("%a %b %e %T %Y")
15       .msg_format("Custom: [{timestamp}] {level} [{file}:{line}] - {args}");
16
17    // Simpler format
18    let mut tl2 = StderrLogger::new(LevelFilter::Info);
19    tl2.msg_format("Simple1: {level} [{timestamp}] {args}")
20       .timestamp_format("%T");
21
22    // Even simpler
23    let mut tl3 = StderrLogger::new(LevelFilter::Debug);
24    tl3.msg_format("Simple2: {level} - {args}")
25       .timestamp_format("");
26
27    // Raw format to stdout
28    let mut tl4 = StdoutLogger::new(LevelFilter::Trace);
29    tl4.msg_format("{args}")
30       .timestamp_format("");
31
32    // File logger
33    let filename = "/tmp/file_logger.log";
34    let mut fl0 = FileLogger::new(LevelFilter::Info);
35    fl0.msg_format("Simple1: {level} [{timestamp}] {args}")
36       .timestamp_format("%T")
37       .filename(filename);
38    println!("Logging to {}", filename);
39    
40    // Create the poly logger and add our logger instances
41    let mut pl = PolyLogger::new();
42    pl.add(tl0);
43    pl.add(tl1);
44    pl.add(tl2);
45    pl.add(tl3);
46    pl.add(tl4);
47    pl.add(fl0.create());
48    pl.init().unwrap();
49
50    trace!("This is an TRACE message");
51    eprintln!("------------------------------");
52    debug!("This is a DEBUG message");
53    eprintln!("------------------------------");
54    info!("This is an INFO message");
55    eprintln!("------------------------------");
56    warn!("This is a WARN message");
57    eprintln!("------------------------------");
58    error!("This is an ERROR message");
59}
Source

pub fn create(&self) -> GenLogger<File>

We need to call this to get a Log interface object such as when passing to PolyLogger. If this is a standalone logger, create() will be called when do the init().

Examples found in repository?
examples/doc_poly.rs (line 12)
4fn main() {
5    let mut stderr_log = StderrLogger::new(LevelFilter::Debug);
6    stderr_log.msg_format("{args}");
7
8    let mut file_log = FileLogger::new(LevelFilter::Info);
9    file_log.filename("./test.log");
10
11    let mut poly_log = PolyLogger::new();
12    poly_log.add(file_log.create()); // create() returns the GenLogger
13    poly_log.add(stderr_log);
14    poly_log.init().unwrap();
15
16    info!("This goes to both loggers");
17    debug!("This only goes to stderr");
18}
More examples
Hide additional examples
examples/poly.rs (line 47)
5fn main() {
6    // Create some logger instances and run all through
7    // the PolyLogger
8    
9    // Default format 
10    let tl0 = StderrLogger::new(LevelFilter::Info);
11
12    // Custom format
13    let mut tl1 = StderrLogger::new(LevelFilter::Warn);
14    tl1.timestamp_format("%a %b %e %T %Y")
15       .msg_format("Custom: [{timestamp}] {level} [{file}:{line}] - {args}");
16
17    // Simpler format
18    let mut tl2 = StderrLogger::new(LevelFilter::Info);
19    tl2.msg_format("Simple1: {level} [{timestamp}] {args}")
20       .timestamp_format("%T");
21
22    // Even simpler
23    let mut tl3 = StderrLogger::new(LevelFilter::Debug);
24    tl3.msg_format("Simple2: {level} - {args}")
25       .timestamp_format("");
26
27    // Raw format to stdout
28    let mut tl4 = StdoutLogger::new(LevelFilter::Trace);
29    tl4.msg_format("{args}")
30       .timestamp_format("");
31
32    // File logger
33    let filename = "/tmp/file_logger.log";
34    let mut fl0 = FileLogger::new(LevelFilter::Info);
35    fl0.msg_format("Simple1: {level} [{timestamp}] {args}")
36       .timestamp_format("%T")
37       .filename(filename);
38    println!("Logging to {}", filename);
39    
40    // Create the poly logger and add our logger instances
41    let mut pl = PolyLogger::new();
42    pl.add(tl0);
43    pl.add(tl1);
44    pl.add(tl2);
45    pl.add(tl3);
46    pl.add(tl4);
47    pl.add(fl0.create());
48    pl.init().unwrap();
49
50    trace!("This is an TRACE message");
51    eprintln!("------------------------------");
52    debug!("This is a DEBUG message");
53    eprintln!("------------------------------");
54    info!("This is an INFO message");
55    eprintln!("------------------------------");
56    warn!("This is a WARN message");
57    eprintln!("------------------------------");
58    error!("This is an ERROR message");
59}

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