Search code examples
ruby-on-railsrubyruby-on-rails-4rakerake-task

How to see rake task puts / logs inside rails server?


I've one rake task like this:

task :create_orders, [:my_param] => :environment do |t, args|
    puts "hello"
    Rails.logger.debug "hey"
end

When I called the rake task from a button inside my view, everything inside the rake task run like what I want.

But, I can't see what I've put inside the puts and Rails.logger.debug inside my terminal where I run the rails server command to start the localhost server.

I also try to run tail -f log/development.log inside my project folder but nothing puts or Rails.logger.debug statements from my rake task showing up.

What should I do so that I can see what I've put inside puts and Rails.logger.debug statements inside my rails server terminal?


Solution

  • I put this on very top of my controller:

    require 'rake'
    Rails.application.load_tasks
    

    And inside the action that calling the rake task, I replaced %x(bundle exec rake task_name) with Rake::Task["task_name"].invoke.

    Thanks mudasobwa for the help!