开发者

Disposing of Log4perl logger when I no longer need it

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 logger开发者_运维百科s 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?")


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


It looks like

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

call should remove everything initialized so far. It should remove any associated resources.


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

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜