How to reopen the output file of a FileLogWriter after Logger::add_writer took it over?
brainpower opened this issue · 2 comments
brainpower commented
let filespec = FileSpec::try_from("access.log")?.suppress_timestamp();
let awriter = FileLogWriter::builder(filespec).append().try_build()?;
let logger_handle = Logger::try_with_env_or_str("info")?
.add_writer("AccessLog", awriter)
.start()?;
In this code which was derived from the example here,
awriter
gets moved into the Logger by add_writer.
Later I want to tell the awriter
to reopen its output files, when SIGHUP is received but I could not find a way to do this.
Things I've tried:
- LoggerHandle's
reopen_outputfile()
only considers the primary writer
and does not consider/handle writers added by add_writer if I read its source correctly. - I have not found any way to get access to the FileLogWriter again through the LoggerHandle.
- I cannot pass an
ArcLogFileWriter
to add_writer, since it does not implement LogWriter.
So: How can I get the awriter
to reopen its log file in a sighup handler?
fn handle_sighup(lh: LoggerHandle) -> Result<(), Box<dyn std::error::Error>> {
// somehow tell the logger to reopen the logfile of awriter here...
// this does not print any logfiles, even though logging to the file works fine:
eprintln!("logfiles: {:?}", lh.existing_log_files());
// this does not work, it returns NoFileLogger error:
lh.reopen_outputfile()
}
(Sorry, if I'm missing some obvious core rust thing that could be used here to keep a reference to awriter
, I'm fairly new to rust.)
emabee commented
The method LoggerHandle::reopen_output()
(name has slightly changed) now cares for all configured writers.
brainpower commented
Just tried this new method in my project and it worked nicely!
Thanks!