3
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

rails runnerでエラー内容表示

Posted at

rails runnerをたたくと、

Please specify a valid ruby command or the path of a script to run.
Run 'bin/rails runner -h' for help.

なメッセージが。何度試行錯誤しても同じエラーメッセージが出てくるだけ、、、
そして気付いたのが、エラーが起こるとつねに上のメッセージが出てくるのでは?ということ。これでは肝心のエラー内容がわからない、、
そこで、exceptionを捕捉して明示的にloggerでエラー内容を出力することにした。見事解決。

    logger = Logger.new(Rails.root.join('log', "#{Rails.env}.log"))

    begin
      logger.info("Start")
    rescue => e
      logger.error("Unhandled exception! #{e} : #{e.backtrace.inject(result = "") { |result, stack| result += "from:#{stack}\n" }}")
    ensure
      logger.info("Finish")
    end

rails runnerは色々ちょっと使いづらい気がする。バッチ処理をするのに、もっと良い方法はないのかなー?

3
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?