I have some Rake tasks that produce CSV output which I’d like to redirect to a file and open with other tools, but when I run heroku rake foo > foo.csv I get log messages (SQL queries, etc.) in my output.
I’ve tried Rails.logger = Logger.new('/dev/null') and Rails.logger = Logger.new(STDERR) at the top of the Rake task and while those function as expected locally, they don’t have any noticeable effect when I run the task on Heroku.
I’m not too shocked that Heroku would squash STDOUT and STDERR together but it’s a mystery to me why sending to /dev/null would not kill the output.
Any help greatly appreciated.
Rails v3.0.0, Heroku bamboo-ree-1.8.7 stack, rake 0.9.2.
From Heroku | Dev Center | Logging:
I think Heroku includes (in the output sent through your
git pushcommand) a notification about this (and one other addition: for serving static/publiccontent, if I remember correctly). You may only see the notifications for certain kinds of pushes though (complete slug rebuilds?). I remember seeing it when I recently pushed a new application to a Bamboo/MRI-1.9.2 stack, but I do not think I got the message every time I pushed changes to just the application’s code (maybe adding a new gem to the Gemfile is enough to trigger it?).Several Rails subsystems keep their own
loggerbinding (independent bindings whose values are often initialized fromRails.logger; reassigning the latter does not change the former):ActiveRecord::Base.loggerActionController::Base.loggerActionMailer::Base.loggerHeroku’s changes probably set a new value for
Rails.loggerbeforeActiveRecordis initialized. WhenActiveRecordis eventually loaded, it sets its ownloggerto be the same asRails.logger(the Heroku/stdout one). When your task runs, it reassignsRails.logger, but it is too late for this to have any effect onActiveRecord::Base.logger(the only most likely to be handling the SQL logs).You probably need to reassign some of these other
loggerbindings to squelch the logging going to STDOUT. Some other likely locations are listed in rails_log_stdout’sinit.rbin the Rails 2 section.