0
我正在爲我的Rails應用程序處理錯誤日誌記錄系統,以找出任何隱藏的錯誤。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
對象上是否有屬性?
我也想過通過正則表達式來過濾行。但它沒有高性能成本? –
在正常使用情況下不應發生錯誤。理想情況下,通常情況下,它們只在開發過程中發生,或者在生產過程中偶爾發生。因此,執行錯誤操作通常不是問題。此外,回溯通常不超過幾百行。正則表達式匹配這樣的行不會是一個主要的性能損失。 – sawa