Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to make a Handler recognize that it had been removed?

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

like image 839
Uri Avatar asked Jan 28 '16 21:01

Uri


1 Answers

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.

  1. 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.

  2. 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.

like image 94
jmehrens Avatar answered Oct 02 '22 14:10

jmehrens