Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Any way to log to a file? #125

Closed
bbigras opened this issue Jan 18, 2019 · 11 comments
Closed

Any way to log to a file? #125

bbigras opened this issue Jan 18, 2019 · 11 comments

Comments

@bbigras
Copy link

bbigras commented Jan 18, 2019

Any way to log to a file?

If not could it be a feature request?

@KodrAus
Copy link
Collaborator

KodrAus commented Jan 19, 2019

Hi @bbigras 👋

You should be able to pipe the output stream from your application into a file.

env_logger doesn't support logging to files as a first-class thing. It's actually a bit tricky to do in a robust way so we consider file logging out-of-scope for this library.

@bbigras
Copy link
Author

bbigras commented Jan 21, 2019

You should be able to pipe the output stream from your application into a file.

Yes this is enough when running in docker, systemd or as a Windows service with NSSM. However I don't know how to pipe the output stream on Windows for a non-console application.

Do you know by chance if there's a simple way? With a Windows shortcut maybe. I would prefer not having to use a wrapper program.

@KodrAus
Copy link
Collaborator

KodrAus commented Jan 30, 2019

Ah I'm not too sure, sorry. I think if you want to log to a file without any kind of sidecar or wrapper around your application you might be better off looking at a framework like log4rs, which has support for logging to rolling files.

@KodrAus
Copy link
Collaborator

KodrAus commented Feb 5, 2019

It's been a little while so I'll go ahead and close this one now as something we won't be supporting in a first-class feature in env_logger itsef. If you've got any other questions though please feel free to open new issues!

@KodrAus KodrAus closed this as completed Feb 5, 2019
@osa1
Copy link

osa1 commented Sep 29, 2019

Just came here to ask for this and found this issue -- I think it'd be good to mention that logging to a file is out of scope for this library as I think this'd be a common request from users.

@vext01
Copy link

vext01 commented Jan 17, 2023

I know it's been deemed out of scope, but thought I'd share a scenario where logging to file is necessary, and where I would have liked to have used envlogger.

I have a scenario where I have a langtester test suite that checks stdout/stderr of test binaries.

During debugging I need to be able to do "print debugging", but printing to either stdout/stderr will make tests fail. The only solution is to log to a file.

@vdominguez1993
Copy link

vdominguez1993 commented Jan 27, 2023

I know it's been deemed out of scope, but thought I'd share a scenario where logging to file is necessary, and where I would have liked to have used envlogger.

I have a scenario where I have a langtester test suite that checks stdout/stderr of test binaries.

During debugging I need to be able to do "print debugging", but printing to either stdout/stderr will make tests fail. The only solution is to log to a file.

I came across the same issue, but actually there's a really easy solution.

Configuring the target as pipe you can pass your file.
Let me share a working example that I'm using:

    let target = Box::new(File::create("/tmp/test.txt").expect("Can't create file"));

    Builder::new()
        .format(|buf, record| {
            writeln!(
                buf,
                "{}:{} {} [{}] - {}",
                record.file().unwrap_or("unknown"),
                record.line().unwrap_or(0),
                Local::now().format("%Y-%m-%dT%H:%M:%S%.3f"),
                record.level(),
                record.args()
            )
        })
        .target(env_logger::Target::Pipe(target))
        .filter(None, LevelFilter::Info)
        .init();

If you need a more complex logger you can pass your own struct implementing std::io::Write.

@calmofthestorm
Copy link

Note that you may need to update to 0.9.3+ to make use of the above solution due to #208.

@sweihub
Copy link

sweihub commented Jun 8, 2023

Thanks, working example

use chrono::Local;
use log::*;
use std::fs::File;
use std::io::Write;

fn main() {
    let target = Box::new(File::create("log.txt").expect("Can't create file"));

    env_logger::Builder::new()
        .target(env_logger::Target::Pipe(target))
        .filter(None, LevelFilter::Debug)
        .format(|buf, record| {
            writeln!(
                buf,
                "[{} {} {}:{}] {}",
                Local::now().format("%Y-%m-%d %H:%M:%S%.3f"),
                record.level(),
                record.file().unwrap_or("unknown"),
                record.line().unwrap_or(0),
                record.args()
            )
        })
        .init();

    debug!("debug line");
    info!("Hello world");
    info!("Hello world");
    info!("Hello world");
    info!("Hello world");
}

@sweihub
Copy link

sweihub commented Jun 11, 2023

Even simpler out-of-the-box async crate: log2

use log2::*;

fn main() {
    let _log2 = log2::open("log.txt").start();

    trace!("send order request to server");
    debug!("receive order response");
    info!("order was executed");
    warn!("network speed is slow");
    error!("network connection was broken");
}

Output logs (tee enabled)

log2 screenshot

Outpu files

log.txt
log.1.txt
log.2.txt
log.3.txt
log.4.txt
log.5.txt
log.6.txt
log.7.txt
log.8.txt
log.9.txt

@mozboz
Copy link

mozboz commented Oct 9, 2023

I know it's been deemed out of scope, but thought I'd share a scenario where logging to file is necessary, and where I would have liked to have used envlogger.

I have a scenario where I have a langtester test suite that checks stdout/stderr of test binaries.

During debugging I need to be able to do "print debugging", but printing to either stdout/stderr will make tests fail. The only solution is to log to a file.

Another example is TUI's where logging output will interfere with the UI

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants