Recycling Log4perl logger when I no longer need it

I use Log4perl as part of a package to capture what a particular DBI connection does. My current plan is to create a new log object for each connection via Log::Log4perl->get_logger($mysql_connect_id) , which should allow different connections to write to different files or the same file without screwing each other.

My concern is what happens when the connection is disconnected and that the registrar is no longer needed. If Log4perl just keeps these logs indefinitely, it sounds like a memory leak recipe.

What is the best way to get rid of the registrar after I am sure that it is no longer useful? Or, on the contrary, is this even a problem - does Log4perl have some kind of built-in removal mechanism that already prevents such leaks?


Edit: Mentioned in the comments on the question, it might be worth mentioning here: Log :: Log4perl :: Logger has a DESTROY method that seems promising. However, it is undocumented and gives a bunch of warnings "Using uninitialized value in string equalizers", which makes me wary; it looks like a hack. (But if this is the best / only way to do this, I assume the question would be "How to disable a specific warning coming from a specific package?")
+4
source share
4 answers

The only way I can see is to manipulate the internal cache of Log::Log4perl::Logger .

 delete $Log::Log4perl::Logger::LOGGERS_BY_NAME->{$category}; 

This is “safe” because it will work with current versions of Log :: Log4perl, but not safe because it may break in the update. This was previously suggested by another SO user, but they removed it.

I suggest you make a function request to delete individual cache entries as part of the API. If you want to speed it up, send the patch. It is pretty simple.

+5
source

Sorry for the delay, I think I finally fixed it. Now you can use the just implemented Log::Log4perl->remove_logger($logger) method to remove unused loggers (remember to remove the remaining $logge r link that you are holding).

Tested on github and should come out with the next version (1.33). Thank you for bringing this to my attention. Your magazine guy, Mike.

+3
source

Similar to

 Log::Log4perl::Logger->cleanup(); 

The call should remove all initialized so far. It should remove any related resources.

+2
source

From the docs at http://search.cpan.org/dist/Log-Log4perl/lib/Log/Log4perl.pm :

To remove the logger from the system, use Log :: Log4perl-> remove_logger ($ logger). After the remaining reference $ logger leaves, the logger will self-destruct. If the registrar in question is a stealth registrar, all its convenient combinations (DEBUG, INFO, etc.) will turn into no-ops.

0
source

All Articles