rust-cli / env_logger

A logging implementation for `log` which is configured via an environment variable.
https://docs.rs/env_logger
Apache License 2.0
813 stars 126 forks source link

method not found in `&mut Formatter` #287

Closed BlackAsLight closed 10 months ago

BlackAsLight commented 10 months ago
Builder::new()
    .filter_level(Info)
    .write_style(Never)
    .parse_default_env()
    .format_indent(Some(4))
    .format(|buf, record| {
        writeln!(
            buf,
            "[{}] [{}] [{}] [{:?}] [{:?}] {}",
            buf.timestamp(),
            record.level(),
            record.target(),
            record.module_path(),
            record.file(),
            record.args()
        )
    })
    .target(Target::Pipe(Box::new({
        let log_dir = Path::new("./logs/");
        if !log_dir.exists() {
            fs::create_dir_all(log_dir).expect("Unable to create ./logs/");
        }

        let mut counter = 0;
        while Path::new(&format!("./logs/latest.{}.log", counter)).exists() {
            counter += 1;
        }
        File::create(&format!("./logs/latest.{}.log", counter)).expect(&format!("Unable to create ./logs/latest.{}.log", counter))
    })))
    .init();
cargo +nightly check --no-default-features --features=server
    Checking idiot_box v0.1.0 (/Users/soul/Projects/IdiotBox)
error[E0599]: cannot write into `&mut env_logger::fmt::Formatter`
  --> src/main.rs:51:6
   |
50 | /                 writeln!(
51 | |                     buf,
   | |                     ^^^
52 | |                     "[{}] [{}] [{}] [{:?}] [{:?}] {}",
53 | |                     buf.timestamp(),
...  |
58 | |                     record.args()
59 | |                 )
   | |_________________- method not found in `&mut Formatter`
   |
note: must implement `io::Write`, `fmt::Write`, or have a `write_fmt` method
  --> src/main.rs:51:6
   |
51 |                     buf,
   |                     ^^^
   = help: items from traits can only be used if the trait is in scope
help: the following trait is implemented but not in scope; perhaps add a `use` for it:
   |
1  + use std::io::Write;
   |

For more information about this error, try `rustc --explain E0599`.
error: could not compile `idiot_box` (bin "idiot_box") due to previous error

I'm getting this weird error in my code. I don't understand why it is throwing this error as when I view the source of Formatter, it looks implemented to me.

BlackAsLight commented 10 months ago

I figured out what I was doing wrong. I needed to include use std::io::Write; at the top of my file.