Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disposing of Log4perl logger when I no longer need it

Tags:

perl

log4perl

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

My concern is over what happens when the connection is disconnected and that logger is no longer needed. If Log4perl just keeps these loggers around indefinitely, that sounds like a recipe for a memory leak.

What's the best way to get rid of a logger after I'm sure it's no longer useful? Or, conversely, is this even a problem -- does Log4perl have some sort of built-in disposal mechanism that already prevents this sort of leak?


Edit: Mentioned in a question's comments, probably worth mentioning here: Log::Log4perl::Logger has a DESTROY method that seems promising. However, it's undocumented and throws a bunch of "Use of uninitialized value in string eq" warnings, which makes me wary; it feels like a hack. (But if that IS the best/only way to do it, I suppose the question becomes "How do I turn off a specific warning coming from a specific package?")
like image 541
BlairHippo Avatar asked Dec 28 '22 21:12

BlairHippo


2 Answers

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

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

This is "safe" in that it will work with the current versions of Log::Log4perl, but not safe in that it could break in an update. This was previously suggested by another SO user but they deleted it.

I would suggest you make a feature request for the ability to delete individual cache entries as part of the API. If you want to expedite it, submit a patch. It's pretty straightforward.

like image 82
Schwern Avatar answered Jan 28 '23 09:01

Schwern


Sorry for the delay, I think I've finally fixed it now. You can now use a newly implemented method Log::Log4perl->remove_logger($logger) to delete unused loggers (don't forget to nuke the remaining reference of $logger you're holding).

Checked into github and should go out with the next release (1.33). Thanks for bringing this to my attention. Your log4perl guy, Mike.

like image 45
Mike Schilli Avatar answered Jan 28 '23 09:01

Mike Schilli