I am writing a custom log configuration class that sets up a particular handler and associates it with the root logger, and plan to use it in multiple applications. I am concerned that actual program code will remove that handler and install a different one.
Is there a way that a handler can detect that it had been removed from a particular logger, or for a logger to report that associates have changed?
My only other alternative is to have a thread that will regularly poll the root loggers handlers and reconnect this handler, which is extremely ugly
Is there a way that a handler can detect that it had been removed from a particular logger, or for a logger to report that associates have changed?
In order to produce subclasses of Logger you have to create a custom LogManager. In some environments expect a specific log manager so that may not work.
However, there are a few hacky things that you can try.
Modifying the logger handlers requires logging permission. Run under a SecurityManager that either prevents other threads from access or create a custom SecurityManager that fires some callback when logging control is requested.
If you can get past this being highly implementation specific and fragile you can use the fact that Logger.removeHandler relies on calling Handler.equals(Object). Override equals in your custom handler implementation and use the Throwable.getStackTrace() to examine the frames to check if equals was called from removeHandler. If it was then simply return false.
My only other alternative is to have a thread that will regularly poll the root loggers handlers and reconnect this handler, which is extremely ugly
Get ready for extremely ugly.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With