ruby-on-rails - 如果我在应用程序 Controller 中处理错误,则在日志中打印异常

标签 ruby-on-rails ruby error-handling


你好我的代码在这里

class ApplicationController < ActionController::Base
  protect_from_forgery

  rescue_from Exception, :with => :error_render_method

   def error_render_method
    respond_to do |type|
      type.html { render :template => "web/static/missing", :status => 404 }
      type.all  { render :nothing => true, :status => 404 }
    end
    true
  end
end

它工作正常,但如果发生任何错误我也想在日志中打印我该怎么做

最佳答案

使用logger.warn :

rescue_from Exception do |exception|
   error_render_method(exception)
end

def error_render_method(error)
  logger.warn(error.message)
  respond_to do |type|
    type.html { render :template => "web/static/missing", :status => 404 }
    type.all  { render :nothing => true, :status => 404 }
  end
  true
end

关于ruby-on-rails - 如果我在应用程序 Controller 中处理错误,则在日志中打印异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24163992/

相关文章:

ruby-on-rails - 我怎样才能在 ActiveAdmin/Arbre 中有一个有多个 child 的 div

ruby - 在 Ruby 中添加到数组中的每个元素

python - 如何解决 “ValueError: expected square matrix”问题?

ruby-on-rails - Redmine 插件仅出现在某些项目菜单中

javascript - 如果在js.erb中确认

ruby-on-rails - Rails 路由到错误的 Controller 操作

ruby-on-rails - 事件管理员搜索状态区域设置不起作用

ruby - ruby 存在哪个框架仅用于服务 SOA 和接受模型的 MySQL(如 MVC 中的 M)?不需要V和C

c# - 处理 Action 过滤器和 Orchard 框架的异常

error-handling - 仅返回GraphQL中的子解析器错误