简体   繁体   中英

Joining separate log to main Rails development log

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).

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

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

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.

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.

尝试将以下行添加到config / initializers / delayed_job_config.rb

Delayed::Worker.logger = Logger.new(STDOUT)

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!).

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 .

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM