Closed yaoxin1995 closed 8 months ago
You can create your own newtype logger which allows switching between different loggers:
struct CustomLogger(std::sync::Mutex<Box<dyn Logger + Send + Sync + 'static>>);
impl Logger for CustomLogger {
// ...
}
Hi, I'm curious why log doesn't support reset the global logger, it's easy to remove the STATE check, so I'm sure I didn't get the real reason. Is there anything I should pay attention to if I want to implement it myself? Thanks.
Hi, I'm curious why log doesn't support reset the global logger, it's easy to remove the STATE check, so I'm sure I didn't get the real reason. Is there anything I should pay attention to if I want to implement it myself? Thanks.
Because the logger
function returns a static lifetime, see https://github.com/rust-lang/log/blob/cab10885defdf445dc730e5a22f67fcec69ca728/src/lib.rs#L1458
If the logging implementation could be de-initialised or switched it could means that the logger would be deallocated while references to it still exists.
A solution for this could be to implement your own logger switcher, e.g. using RwLock<MyLogger>
. Of course this will have an impact on performance.
Hi, I'm curious why log doesn't support reset the global logger, it's easy to remove the STATE check, so I'm sure I didn't get the real reason. Is there anything I should pay attention to if I want to implement it myself? Thanks.
Because the
logger
function returns a static lifetime, seehttps://github.com/rust-lang/log/blob/cab10885defdf445dc730e5a22f67fcec69ca728/src/lib.rs#L1458
If the logging implementation could be de-initialised or switched it could means that the logger would be deallocated while references to it still exists.
A solution for this could be to implement your own logger switcher, e.g. using
RwLock<MyLogger>
. Of course this will have an impact on performance.
Thank you!
P.S. If your log implementation can operate on immutable reference or uses fine-grand locking inside, then you could use ArcSwap
which is optimized for read-heavy (immutable-use heavy) scenario.
P.S. If your log implementation can operate on immutable reference or uses fine-grand locking inside, then you could use
ArcSwap
which is optimized for read-heavy (immutable-use heavy) scenario.
Thanks for your advice!
Hi @yaoxin1995 /@Geass-LL!
I share the same reconfiguration demand and created a solution. Checkout out this crate: https://github.com/brummer-simon/alterable_logger
Closing this as not possible with the current design.
Hey, I would like to reset the global logger (replace the old one with a new logger) as follows:
But I got the error
SetLoggerError(())
.Do you know how I can replace the logger?