emabee / flexi_logger

A flexible logger for rust programs that can write to stderr, stdout, and/or to log files
Apache License 2.0
307 stars 50 forks source link

Ability to silence errors from flexi_logger itself? #100

Closed redzic closed 2 years ago

redzic commented 2 years ago

I have a use case where my log file isn't created until some time, and flexi_logger writes to both the logger and to stderr. While the file is not created yet, I would like for flexi_logger to log to just stderr and ignore the errors for the main log file not being created yet. While this is working perfectly, flexi_logger displays an error message every time something is logged, which looks like this:

[flexi_logger][ERRCODE::Write] writing failed, caused by No such file or directory (os error 2)

I think it would be great if there was some way to manually handle these errors rather than it always be printed to stderr. I should clarify that this only happens when the log file is supposed to be located in a directory that does not yet exist. If the parent directory does exist, then everything works completely fine.

emabee commented 2 years ago

I'm planning an option to configure the output channel for flexi_logger's own error messages, like the one above. With that, you could direct these messages e.g. to a file, or to /dev/null. Would that be good enough?

emabee commented 2 years ago

I should clarify that this only happens when the log file is supposed to be located in a directory that does not yet exist

This is strange - flexi_logger usually creates the missing folders. This is also tested regularly. Is there some authorization issue that prevents the process from creating the folders?

emabee commented 2 years ago

Silencing errors is now possible now with 0.20.1, using .error_channel(ErrorChannel::DevNull)

redzic commented 2 years ago

Thanks! Sorry for the somewhat late response, but yeah that is a good solution to me. Also I sort of was able to fix it, but short story is that apparently if the log file failed to be created initially, (I guess) flexi_logger doesn't try again after that which was the source of our errors (a different error where the log file wasn't created at all).

Thanks for this great crate!