Take the 2-minute tour ×

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?")

share|improve this question
DESTROY method should not be called manually, because it'll be caller by perl itself when object is going to be removed from memory. Calling DESTROY directly will not free the memory. –  Ivan Nevostruev May 6 '11 at 17:09
@Ivan: Well, crap. So much for a perfectly good-looking hack. Thanks for the tip. –  BlairHippo May 6 '11 at 17:14
Logger objects are a small hash of code references. Unless you're making tons of the things, I'd posit the extra memory is inconsequential compared to whatever actual data your program is storing in memory. Don't worry about it. –  Schwern May 9 '11 at 6:38
add comment

4 Answers

up vote 4 down vote accepted

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.

share|improve this answer
I've handled the problem by designing around it, but if I were still locked into the original design, this is the solution I'd use. And it's a good thing to have in my pocket. Thanks! (BTW -- I think we know each other in RL.) –  BlairHippo May 10 '11 at 19:53
add comment

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.

share|improve this answer
add comment

It looks like


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

share|improve this answer
Nuke the site from orbit? :-) That'd do it, but if I'm reading that correctly, that'll get rid of any loggers I want to keep around, too. –  BlairHippo May 6 '11 at 19:56
@BlairHippo - did read your question incorrectly, I thought you want to get rid of Log::Log4perl completely. If you want to remove just something, why not just give it new config and let itself to reinitialize? –  bvr May 7 '11 at 8:52
It's unfortunate that cleanup() doesn't just delete its own cache, it aggressively calls DESTROY on each logger. This is unnecessary and makes calling cleanup() dangerous. You have to know that all loggers everywhere in the entire process, even in 3rd party modules, are no longer in use. –  Schwern May 9 '11 at 6:47
I've reported the problem of cleanup's aggressive behavior to the author. github.com/mschilli/log4perl/issues/7 (Also, cleanup is not documented so you shouldn't be using it) –  Schwern May 9 '11 at 7:01
add comment

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


share|improve this answer
Heh. If you read the other answers, that feature is a consequence of this question. I'm actually kinda proud. :-) –  BlairHippo Dec 2 '11 at 16:58
add comment

Your Answer


