Like any Rails application, we write quite a few rake tasks to automate activities. Rake is great, but I’ve always been frustrated with the logging. We would use puts in the rake tasks, but other components that used Rails.logger wouldn’t show up in the console - they were going to [environment].log.

So I created a simple rake task to switch the logging to STDOUT:

#Sets up logging - should only be called from other rake tasks
task setup_logger: :environment do
  logger           = Logger.new(STDOUT)
  logger.level     = Logger::INFO
  Rails.logger     = logger
end

Because this picks up our typical dependency - environment - I can replace environment with setup_logger:

desc <<-DESC
Creates a job: outreach:jobs:create[<job>,<org>,<args>]
Ex: outreach:jobs:create[distribute,community,job_name:close_cases,immediate:true]
DESC
task :create, [:job_name, :org_short_name] => :setup_logger do |_, args|
  JobLauncher.launch args[:job_name], args[:org_short_name], parse_extras(args)
end

This seems like an obvious fix, but I haven’t seen it used before. It’s working great for us, hopefully it will help someone else.

comments powered by Disqus