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

How to reopen the output file of a FileLogWriter after Logger::add_writer took it over? #143

Closed
brainpower opened this issue Aug 10, 2023 · 2 comments

Comments

@brainpower
Copy link

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
Copy link
Owner

emabee commented Sep 1, 2023

The method LoggerHandle::reopen_output() (name has slightly changed) now cares for all configured writers.

@emabee emabee closed this as completed Sep 1, 2023
@brainpower
Copy link
Author

Just tried this new method in my project and it worked nicely!
Thanks!

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

2 participants