Why is the frame not called to implement the registrar?

I am trying to use a log box and implement a logger myself. I call it mylog .

 extern crate log; use log::*; struct Mylog; impl log::Log for Mylog { fn enabled(&self, metadata: &LogMetadata) -> bool { metadata.level() <= LogLevel::Info } fn log(&self, record: &LogRecord) { if self.enabled(record.metadata()) { println!("hello log"); } } } impl Drop for Mylog { fn drop(&mut self) { println!("dropped"); // This is never called, why? } } pub fn init() -> Result<(), SetLoggerError> { log::set_logger(|max_log_level| { max_log_level.set(LogLevelFilter::Info); Box::new(Mylog) }) } 

And in main.rs:

 extern crate mylog; #[macro_use] extern crate log; fn main() { mylog::init().unwrap(); info!("My info message"); } 

Drop never called, and I don't understand why.

+7
logging destructor rust
source share
1 answer

The registrar implementation is provided to the registration library and is effectively leaked . This allows the implementation to act as if it has a 'static lifetime, allowing it to be used in many places.

If you really need it, you can close the logger at the end of the program:

 fn main() { mylog::init().unwrap(); info!("My info message"); log::shutdown_logger(); } 
+7
source share

All Articles