-
Notifications
You must be signed in to change notification settings - Fork 130
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
Comments
Hi @bbigras 👋 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. |
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 |
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 |
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. |
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.
If you need a more complex logger you can pass your own struct implementing std::io::Write. |
Note that you may need to update to 0.9.3+ to make use of the above solution due to #208. |
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");
} |
Even simpler out-of-the-box async crate: 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) Outpu files
|
Another example is TUI's where logging output will interfere with the UI |
Any way to log to a file?
If not could it be a feature request?
The text was updated successfully, but these errors were encountered: