如何使用 Ruby 和 eventmachine 进行日志记录?
我正在使用 Ruby 和 Eventmachine 库编写一个应用程序。我真的很喜欢非阻塞 I/O 和事件驱动系统的想法,我遇到的问题是日志记录。我正在使用 Ruby 的标准记录器库。这并不是说日志记录需要永远进行,但它似乎不应该被阻止,但它确实被阻止了。是否有一个库可以将 Ruby 的标准记录器实现扩展为非阻塞,或者我应该只调用 EM::defer 来进行日志记录调用?有没有办法让 eventmachine 为我做这件事?
I'm writing an application using Ruby and the Eventmachine library. I really like the idea of non blocking I/O and event driven systems, the problem I'm running into is logging. I'm using Ruby's standard logger library. Its not that logging takes forever but it seems like something that shouldn't block and it does. Is there a library out there somewhere that extends Ruby's standard logger implementation to be non-blocking or should I just call EM::defer for my logging calls? Is there a way I can make eventmachine do this for me already?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我最终将记录器包装在一个启动线程并具有 FIFO 队列的单例类中。日志记录会将日志信息转储到队列中,线程只是循环,从队列中取出内容并使用真正的记录器来记录它。不是真正的反应器模式,但它也不会占用我的 EM 线程池。
对于这个单例,你只能拥有一个记录器,但这是我在 github 上所做的。
I ended up wrapping the logger in a singleton class that started a thread and had a FIFO queue. Logging would dump log info into the queue and the thread just looped, pulling stuff out of the queue and using the real logger to log it. Not really reactor pattern but it won't chew up my EM thread pool either.
With this singleton, you can only ever have one logger but here is what I did up on github.
如果您使用的是带有 Syslog 的系统,您可以查看 EM-Syslog
If you're on a system with Syslog you can take a look at EM-Syslog