当我不再需要 Log4perl 记录器时处置它

发布于 2024-11-06 00:48:00 字数 779 浏览 1 评论 0原文

我使用 Log4perl 作为包的一部分来捕获特定 DBI 连接正在执行的操作。我当前的计划是通过 Log::Log4perl->get_logger($mysql_connect_id) 为每个连接创建一个新的记录器对象,这应该允许各种连接写入不同的文件或相同的文件文件,而不会互相搞砸。

我担心的是当连接断开并且不再需要该记录器时会发生什么。如果 Log4perl 只是无限期地保留这些记录器,这听起来像是内存泄漏的根源。

当我确定记录器不再有用后,摆脱它的最佳方法是什么?或者,相反,这甚至是一个问题——Log4perl 是否有某种内置的处理机制已经可以防止这种泄漏?


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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

停滞 2024-11-13 00:48:00

我看到的唯一方法是操作 Log::Log4perl::Logger 的内部缓存。

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

这是“安全的”,因为它可以与 Log::Log4perl 的当前版本一起使用,但不安全,因为它可能会在更新中中断。这是另一个 SO 用户之前建议的,但他们删除了它。

我建议您提出功能请求,以获取作为 API 一部分删除单个缓存条目的功能。如果您想加快速度,请提交补丁。这非常简单。

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.

七颜 2024-11-13 00:48:00

抱歉耽搁了,我想我现在终于修好了。您现在可以使用新实现的方法 Log::Log4perl->remove_logger($logger) 来删除未使用的记录器(不要忘记删除 $logge 的剩余引用>你拿着)。

已签入 github 并应随下一个版本 (1.33) 一起发布。感谢您让我注意到这一点。你的 log4perl 人员,Mike。

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.

夜唯美灬不弃 2024-11-13 00:48:00

看起来

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

call 应该删除到目前为止初始化的所有内容。它应该删除任何关联的资源。

It looks like

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

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

梦魇绽荼蘼 2024-11-13 00:48:00

来自 http://search.cpan.org/dist 的文档/Log-Log4perl/lib/Log/Log4perl.pm

要从系统中删除记录器,请使用 Log::Log4perl->remove_logger($logger)。剩余的引用 $logger 消失后,记录器将自毁。如果相关记录器是隐形记录器,则其所有便利快捷方式(DEBUG、INFO 等)都将变为无操作。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文