user7055375
user7055375

Reputation:

How do I send my output to the Rails console?

I'm using Rails 5 and Ruby 2.4. I would like to capture the output from an error, print it on my console, and so I have in one of my app/service classes

rescue => e
  error = e
  msg = "#{e.message}\n"
  e.backtrace.each { |line|
    msg = "#{msg}\n#{line}"
  }
  logger.error msg
  raise e
end

but I'm getting the below error

NameError: undefined local variable or method `logger' for #<RunCrawlersService:0x007fd14c2f1e10>
    from /Users/nataliab/Documents/workspace/myproject/app/services/run_mycustom_service.rb:34:in `rescue in block (2 levels) in run_all_crawlers'
    from /Users/nataliab/Documents/workspace/myproject/app/services/run_mycustom_service.rb:16:in `block (2 levels) in run_all_crawlers'
    from /Users/nataliab/.rvm/gems/ruby-2.4.0/gems/concurrent-ruby-1.0.5/lib/concurrent/executor/ruby_thread_pool_executor.rb:348:in `run_task'
    from /Users/nataliab/.rvm/gems/ruby-2.4.0/gems/concurrent-ruby-1.0.5/lib/concurrent/executor/ruby_thread_pool_executor.rb:337:in `block (3 levels) in create_worker'
    from /Users/nataliab/.rvm/gems/ruby-2.4.0/gems/concurrent-ruby-1.0.5/lib/concurrent/executor/ruby_thread_pool_executor.rb:320:in `loop'
    from /Users/nataliab/.rvm/gems/ruby-2.4.0/gems/concurrent-ruby-1.0.5/lib/concurrent/executor/ruby_thread_pool_executor.rb:320:in `block (2 levels) in create_worker'
    from /Users/nataliab/.rvm/gems/ruby-2.4.0/gems/concurrent-ruby-1.0.5/lib/concurrent/executor/ruby_thread_pool_executor.rb:319:in `catch'
    from /Users/nataliab/.rvm/gems/ruby-2.4.0/gems/concurrent-ruby-1.0.5/lib/concurrent/executor/ruby_thread_pool_executor.rb:319:in `block in create_worker'

How do I properly send my output to the console?

Upvotes: 0

Views: 204

Answers (1)

Andrey Deineko
Andrey Deineko

Reputation: 52367

You want to use Rails.logger:

rescue => e
  Rails.logger.error "#{e.message}\n"
  e.backtrace.each { |line| Rails.logger.error "#{msg}\n#{line}" }
  raise e
end

Upvotes: 3

Related Questions