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
797 stars 124 forks source link

Why no simple file logging example? #233

Closed JonathanWoollett-Light closed 2 years ago

JonathanWoollett-Light commented 2 years ago

It would seem a fairly fundamental use case to simply log to a file.

I tried a simple example via setting the target of the builder as a file:

use env_logger::{fmt::Target, Builder};
use log::error;
fn main() {
    let file = std::fs::OpenOptions::new()
        .create(true)
        .append(true)
        .open("log.txt")
        .unwrap();

    let mut builder = Builder::from_default_env();
    builder.target(Target::Pipe(Box::new(file))).init();
    error!("Some error");
}

But this seems to have had no affect other than implicitly doing builder.write_style(WriteStyle::Never); with the output still being written to the console.

Is there any example of logging to a file?

harlanc commented 1 year ago

You can refer to this crate : https://crates.io/crates/env_logger_extend It can write logs to a specified file and also support setting rotate.