Rails错误处理/记录:简化回溯 [英] Rails error handling/logging: Simplified backtrace

查看:184
本文介绍了Rails错误处理/记录:简化回溯的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为我的Rails应用程序制作一个错误日志系统,以接收任何隐藏的错误。

I am working on an error logging system for my Rails app, to pick up on any hidden bugs.

到目前为止,我我的ApplicationController中已经实现了一个 rescue_from (请注意,我正处于实验阶段,所以我只将结果输出到控制台)

So far, I've implemented this with a rescue_from on my ApplicationController: (note that I'm in the experimental phase so I'm only outputting the results to the console)

   rescue_from Exception, with: :log_exception

   private
    def log_exception exception
      puts("Exception at #{Time.now.strftime('%d %b %Y, %H:%M')}")
      puts exception
      puts exception.backtrace.join "\n"
      raise exception
    end

我遇到的问题是 exception.backtrace 。它输出一大堆无用的深层痕迹,看起来像这样

The issue I'm having is with exception.backtrace. It outputs a whole bunch of useless deep trace that looks like this

/Users/marcoprins/.rvm/gems/ruby-2.0.0-p451/gems/activemodel-3.2.13/lib/active_model/attribute_methods.rb:407:in `method_missing'
/Users/marcoprins/.rvm/gems/ruby-2.0.0-p451/gems/activerecord-3.2.13/lib/active_record/attribute_methods.rb:149:in `method_missing'
/Users/marcoprins/Desktop/Project/tillyoudrop/app/models/order.rb:105:in `cancel!'
/Users/marcoprins/Desktop/Project/tillyoudrop/app/controllers/admin/orders_controller.rb:97:in `cancel'
/Users/marcoprins/.rvm/gems/ruby-2.0.0-p451/gems/actionpack-3.2.13/lib/action_controller/metal/implicit_render.rb:4:in `send_action'
/Users/marcoprins/.rvm/gems/ruby-2.0.0-p451/gems/actionpack-3.2.13/lib/abstract_controller/base.rb:167:in `process_action'
/Users/marcoprins/.rvm/gems/ruby-2.0.0-p451/gems/actionpack-3.2.13/lib/action_controller/metal/rendering.rb:10:in `process_action'
/Users/marcoprins/.rvm/gems/ruby-2.0.0-p451/gems/actionpack-3.2.13/lib/abstract_controller/callbacks.rb:18:in `block in process_action'
/Users/marcoprins/.rvm/gems/ruby-2.0.0-p451/gems/activesupport-3.2.13/lib/active_support/callbacks.rb:436:in `_run__4209411771832668003__process_action__2626133284113442857__callbacks'
/Users/marcoprins/.rvm/gems/ruby-2.0.0-p451/gems/activesupport-3.2.13/lib/active_support/callbacks.rb:405:in `__run_callback'
/Users/marcoprins/.rvm/gems/ruby-2.0.0-p451/gems/activesupport-3.2.13/lib/active_support/callbacks.rb:385:in `_run_process_action_callbacks'
/Users/marcoprins/.rvm/gems/ruby-2.0.0-p451/gems/activesupport-3.2.13/lib/active_support/callbacks.rb:81:in `run_callbacks'

,它会上升几英里。

我想让我的错误日志文件看起来干净可读,所以我想要的是这个例子的第3行和第4行,或者说,Rails错误页面中的输出(见p图片)告诉我确切的部分在我的代码中的错误来自于

I want my error log file to look clean and readable, so all I want is essentialy the 3rd and 4th line of this example, or rather, the exact lines that Rails outputs in the error page, (see picture) the part that tells me exactly where in my code the error has originated from.

如何访问这些行?

推荐答案

异常通过与要提取或忽略的文件/目录的模式匹配来拒绝或选择某些行。例如,如果您想忽略 / gems / 中的步骤,则:

Reject or select certain lines by pattern match with the files/directories you want to extract or ignore. For example, if you want to ignore the steps in /gems/, then:

exception.backtrace.reject{|l| l =~ %r|\A[^:]*/gems/|}

只想从 / app / 目录中的步骤,然后:

If you want only the steps from /app/ directory, then:

exception.backtrace.select{|l| l =~ %r|\A[^:]*/app/|}

其实,您应该使用 backtrace_locations 而不是 backtrace ,这可以直接访问路径而不使用正则表达式,但暂时, backtrace_locations 充满了错误,使用它不实用。

Actually, you should be using backtrace_locations instead of backtrace, which gives direct access to paths without using regexes, but for the time being, backtrace_locations is full of bugs, and it is not practical to use it.

这篇关于Rails错误处理/记录:简化回溯的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆