How do I log the entire trace back of a Ruby exception using the default Rails logger?

后端 未结 7 698
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-31 08:29

I am working on rails project and I am trying to get exceptions to be logged to the rails log files. I know I can call logger.error $! to get the first line of the

7条回答
  •  走了就别回头了
    2021-01-31 09:12

    The way rails does it is

    137             logger.fatal(
    138               "\n\n#{exception.class} (#{exception.message}):\n    " +
    139               clean_backtrace(exception).join("\n    ") +
    140               "\n\n"
    141             )
    
    248       def clean_backtrace(exception)
    249         if backtrace = exception.backtrace
    250           if defined?(RAILS_ROOT)
    251             backtrace.map { |line| line.sub RAILS_ROOT, '' }
    252           else
    253             backtrace
    254           end
    255         end
    256       end
    

提交回复
热议问题