有没有办法更改ActionController::RoutingError
的日志级别?
我想将它从错误更改为警告。谷歌搜索并没有找到解决方案...
我只想将路由错误显示为警告。但不能改变其他伐木行为
这里描述了一种可能的解决方法(或部分解决方法)https://stackoverflow.com/a/5386515/10272,但这并不是我想要的。在这个解决方案中,他们只是为页面未找到错误添加一个全能路由器,然后在catch-all-controller中处理它。但是我希望能够只更改日志严重性,如果可能的话,它似乎更能描述我的意图......
答案 0 :(得分:0)
您可以覆盖Simple Formatter,以便将错误记录为警告。
但是,我不建议采用此路由,因为您最终会记录应用程序将其视为错误/异常的操作的警告消息。更优雅的选择是如您所描述的那样拥有一个包罗万象的页面,控制器捕获异常和记录警告消息。
答案 1 :(得分:0)
我在Rails 5.2中使用了初始化器中的以下代码解决了这个问题:
# /config/initializers/routing_error_logger_defatalizer.rb
# Spammers and script kiddies hit the site looking for exploits
# which blows up our logs as we get a bunch of fatal errors for 404s on eg. "/wp-admin.php"
# This initializer changes the log level of RoutingErrors to 'warn' (so they still appear in logs but are not fatal)
# this means we can use logging notifications on eg. >= error without all the false positives
# ref: https://stackoverflow.com/questions/33827663/hide-actioncontrollerroutingerror-in-logs-for-assets
# ref: https://github.com/roidrage/lograge/issues/146#issuecomment-461632965
if Rails.env.production?
module ActionDispatch
class DebugExceptions
alias_method :old_log_error, :log_error
def log_error(request, wrapper)
if wrapper.exception.is_a? ActionController::RoutingError
# Have prepended "[404]" to the message for easier log searches, otherwise it is the default Rails messaging
# Full message eg. "[404] ActionController::RoutingError (No route matches [GET] \"/wp-login.php\")"
logger(request).send(:warn, "[404] ActionController::RoutingError (#{wrapper.exception.message})")
return
else
old_log_error request, wrapper
end
end
end
end
end
对于Rails 4.2,使用:
if Rails.env.production?
class ActionDispatch::DebugExceptions # Rails 4.2
alias_method :old_log_error, :log_error
def log_error(request, wrapper)
if wrapper.exception.is_a? ActionController::RoutingError
# Have prepended "[404]" to the message for easier log searches, otherwise it is the default Rails messaging
# Full message eg. "[404] ActionController::RoutingError (No route matches [GET] \"/wp-login.php\")"
logger(request).send(:warn, "[404] ActionController::RoutingError (#{wrapper.exception.message})")
return
else
old_log_error request, wrapper
end
end
end
end