rails_semantic_logger利用時に例外をINFOレベルにする
SemanticLoggerのappendersにfilterを追加する方法で制御できる
code:config/initializers/log_level_filter.rb
INFO_LEVEL_EXCEPTIONS = [
ActionController::RoutingError
]
# ログレベルをinfoにする例外を指定する
Rails.application.config.to_prepare do
SemanticLogger.appenders.each do |appender|
appender.filter = lambda do |log|
if INFO_LEVEL_EXCEPTIONS.include?(log.exception.class)
log.level = :info
end
true
end
end
end
本当は以下コードで差し込みたかったができなかった。rails_semantic_loggerはDebugExceptionsをモンキーパッチしていて、ここでinfoにしてもfatalにされる
https://github.com/reidmorrison/rails_semantic_logger/blob/fb9bd702625c4d514c347914f5abf95713cb7d6c/lib/rails_semantic_logger/extensions/action_dispatch/debug_exceptions.rb
code:ruby
class LogLevelInterceptor
INFO_LEVEL_EXCEPTIONS = [
ActionController::RoutingError
]
def call(request, exception)
return unless INFO_LEVEL_EXCEPTIONS.include?(exception.class)
request.set_header(
"action_dispatch.debug_exception_log_level",
Logger::INFO
)
end
end
ActionDispatch::DebugExceptions.register_interceptor do |request, exception|
LogLevelInterceptor.new.call(request, exception)
end
rails