Ruby 守护进程导致 ActiveRecord 记录器 IOError
我目前正在 Ruby 中编写一个项目,该项目使用 ActiveRecord gem 进行数据库交互,并且我尝试使用 ActiveRecord::Base.logger
属性和以下代码
ActiveRecord::Base.logger = Logger.new(File.open('logs/database.log', 'a'))
这对于迁移等来说效果很好(出于某种原因,这似乎需要启用日志记录,因为它在禁用时会给出 NilClass 错误),但是当我尝试运行包含调用 ActiveRecord 对象的线程守护程序的项目时,脚本失败出现以下错误
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/logger.rb:504:in `write': closed stream (IOError)
任何有关如何解决此问题的想法将不胜感激。目前我已经开始查看其他代码,看看人们是否有其他方法以更线程安全的方式实现 ActiveRecord 日志记录
谢谢
帕特里克
I'm writing a project at the moment in Ruby which makes use of the ActiveRecord gem for database interaction and I'm trying to log all the database activity using the ActiveRecord::Base.logger
attribute with the following code
ActiveRecord::Base.logger = Logger.new(File.open('logs/database.log', 'a'))
This works fine for migrations etc (which for some reason seem to require that logging be enabled as it gives a NilClass error when it's disabled) but when I try to run the project which includes a threaded daemon calling the ActiveRecord object the script fails with the following error
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/logger.rb:504:in `write': closed stream (IOError)
Any ideas on how to solve this problem would be greatly appreciated. For the moment I've started to look through other code to see if people have other ways of implementing ActiveRecord logging in a more thread-safe manner
Thanks
Patrick
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我遇到了同样的问题。您需要首先进行守护进程,然后加载 Rails 环境。
I ran into the same issue. You need to daemonize first, and then load the Rails environment.
Delayed_Job 使用了守护进程和 ActiveRecord,
在守护进程之前,获取文件已打开,然后在守护进程中重新打开
the delayed_job have used daemons and activerecord,
before daemonize,get the files have opend,and then reopen in daemonize
事实证明,要使迁移工作,
ActiveRecord::Base.logger
变量不能为 nil,这解释了问题的前半部分。尽管当使用文件而不是 STDERR 时,我仍然无法修复 IOError。It turns out that for migrations to work the
ActiveRecord::Base.logger
variable cannot be nil, which explains the first half of the problem. I am as yet unable to fix the IOError though when a file is used instead of STDERR.