Rails 错误 handling/logging:简化的回溯
Rails error handling/logging: Simplified backtrace
我正在为我的 Rails 应用开发一个 错误记录 系统,以发现任何隐藏的错误。
到目前为止,我已经在我的 ApplicationController 上使用 rescue_from
实现了这个:(请注意,我正处于实验阶段,所以我只将结果输出到控制台)
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
。它输出一大堆无用的深度跟踪,看起来像这样
/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 在错误页面中输出的确切行, (见图)准确地告诉我我的代码错误源自何处的部分。
如何访问这些行? exception
对象上是否有包含它的属性?
拒绝或select 某些与您要提取或忽略的files/directories 模式匹配的行。例如,如果您想忽略 /gems/
中的步骤,则:
exception.backtrace.reject{|l| l =~ %r|\A[^:]*/gems/|}
如果您只需要 /app/
目录中的步骤,则:
exception.backtrace.select{|l| l =~ %r|\A[^:]*/app/|}
实际上,您应该使用 backtrace_locations
而不是 backtrace
,它可以直接访问路径而无需使用正则表达式,但目前 backtrace_locations
充满了错误,而且不实用。
我正在为我的 Rails 应用开发一个 错误记录 系统,以发现任何隐藏的错误。
到目前为止,我已经在我的 ApplicationController 上使用 rescue_from
实现了这个:(请注意,我正处于实验阶段,所以我只将结果输出到控制台)
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
。它输出一大堆无用的深度跟踪,看起来像这样
/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 在错误页面中输出的确切行, (见图)准确地告诉我我的代码错误源自何处的部分。
如何访问这些行? exception
对象上是否有包含它的属性?
拒绝或select 某些与您要提取或忽略的files/directories 模式匹配的行。例如,如果您想忽略 /gems/
中的步骤,则:
exception.backtrace.reject{|l| l =~ %r|\A[^:]*/gems/|}
如果您只需要 /app/
目录中的步骤,则:
exception.backtrace.select{|l| l =~ %r|\A[^:]*/app/|}
实际上,您应该使用 backtrace_locations
而不是 backtrace
,它可以直接访问路径而无需使用正则表达式,但目前 backtrace_locations
充满了错误,而且不实用。