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?
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?")
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.
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 $logge
r 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.
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