将单独的日志连接到主要的Rails开发日志 [英] Joining separate log to main Rails development log

查看:83
本文介绍了将单独的日志连接到主要的Rails开发日志的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这与问题相反我见过几次在其他地方,有人想看看如何从主开发日志中创建另一个独立的Rails日志.由于某种原因,我的Rails应用程序将我的DelayedJob gem的活动记录到了单独的日志(delayed_job.log)中,但是我希望它记录到主development.log文件中.我也正在使用Workless gem和NewRelic,这是否可能相关(尽管我通过删除NewRelic对此进行了尝试,但问题仍然存在).

This is the reverse of the question I have seen several times elsewhere, in which someone wants to see how to create an another, separate Rails log from the main development log. For some reason, my Rails app is logging my DelayedJob gem's activity to a separate log (delayed_job.log), but I want it to log to the main development.log file. I am using the Workless gem and NewRelic as well, should this be potentially relevant (although I experimented on this by removing NewRelic, and the issue still remained).

我不清楚这是怎么发生的.但是,我之前在查看日志中的SQL插入和删除时遇到了一些麻烦,另一个用户请

I'm not clear on how this happened. However, I was having some trouble earlier with seeing SQL insertions and deletions in my log, and another user kindly suggested that I use the following in an initializer file:

if defined?(Rails) && !Rails.env.nil?
  logger = Logger.new(STDOUT)
  ActiveRecord::Base.logger = logger
  ActiveResource::Base.logger = logger
end

这样做之后,我看到了SQL语句,但是在主开发日志中不再看到DelayedJob信息.

Once I did this, I saw the SQL statements, but no longer saw the DelayedJob information in the main development log.

所以我的问题是:如何确保DelayedJob活动日志记录到主开发日志中?我不介意它是否也记录到单独的日志中,但是重要的是我在Mac的控制台中看到了它的活动.

So my question is: How can I make sure that DelayedJob activity logs to the main development log? I don't mind if it also logs to a separate log, but the important thing is that I see its activity in my Mac's console.

如果您想从我的应用中获取更多代码,请告诉我-我们很乐意提供.非常感谢Rails的新手.

Please let me know if you'd like more code from my app - I'd be happy to provide it. Much thanks from a Rails newbie.

推荐答案

我终于可以使用它了.感谢Seamus Abshere在此处的回答.我把他下面发布的内容放在初始化文件中.这有delay_job记录到我的development.rb文件中(huzzah!).

I finally got this to work. All thanks to Seamus Abshere's answer to the question here. I put what he posted below in an initializer file. This got delayed_job to log to my development.rb file (huzzah!).

但是,delay_job仍然没有登录到我的控制台(由于我仍然不明白的原因).我通过打开一个新的控制台选项卡并输入tail -f log/development.log解决了这一问题.

However, delayed_job still isn't logging into my console (for reasons I still don't understand). I solved that by opening a new console tab and entering tail -f log/development.log.

与Seamus的编写不同,auto-flushing=true在Rails 4中已弃用,我的Heroku应用程序崩溃了.我通过将其从初始化程序文件中删除并将其作为config.autoflush_log = true放在我的environments/development.rb文件中来解决此问题.但是,我发现两种冲洗都不是进行这项工作所必需的.

Different from what Seamus wrote, though, auto-flushing=true is deprecated in Rails 4 and my Heroku app crashed. I resolved this by removing it from my initializer file and placing it in my environments/development.rb file as config.autoflush_log = true. However, I found that neither of the two types of flushing were necessary to make this work.

这是他的代码(没有自动刷新):

Here is his code (without the auto-flushing):

file_handle = File.open("log/#{Rails.env}_delayed_jobs.log", (File::WRONLY | File::APPEND | File::CREAT))
# Be paranoid about syncing
file_handle.sync = true
# Hack the existing Rails.logger object to use our new file handle
Rails.logger.instance_variable_set :@log, file_handle
# Calls to Rails.logger go to the same object as Delayed::Worker.logger
Delayed::Worker.logger = Rails.logger

如果以上代码无效,请尝试将Rails.logger替换为RAILS_DEFAULT_LOGGER.

If the above code doesn't work, try replacing Rails.logger with RAILS_DEFAULT_LOGGER.

这篇关于将单独的日志连接到主要的Rails开发日志的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆