Files
danbooru/app/logical/danbooru_logger.rb
evazion e70cae457d application controller: clean up exception handling.
* Simplify code.
* Show backtraces for all users, not just builders.
* Show backtraces only for unexpected server errors (status 5xx), not
  for normal client errors (status 4xx).
* Log expected errors at info level (reduce noise in production logs).
2019-08-13 21:30:20 -05:00

44 lines
1.3 KiB
Ruby

class DanbooruLogger
def self.log(exception, expected: false, **params)
if expected
Rails.logger.info("#{exception.class}: #{exception.message}")
else
backtrace = Rails.backtrace_cleaner.clean(exception.backtrace).join("\n")
Rails.logger.error("#{exception.class}: #{exception.message}\n#{backtrace}")
end
if defined?(::NewRelic)
::NewRelic::Agent.notice_error(exception, expected: expected, custom_params: params)
end
end
def self.initialize(request, session, user)
add_attributes("request.params", request.params)
add_attributes("session.params", session.to_h)
add_attributes("user", { id: user.id, name: user.name, level: user.level_string, ip: request.remote_ip })
end
def self.add_attributes(prefix, hash)
return unless defined?(::NewRelic)
attributes = flatten_hash(hash).transform_keys { |key| "#{prefix}.#{key}" }
::NewRelic::Agent.add_custom_attributes(attributes)
end
private
# flatten_hash({ foo: { bar: { baz: 42 } } })
# => { "foo.bar.baz" => 42 }
def self.flatten_hash(hash)
hash.each_with_object({}) do |(k, v), h|
if v.is_a?(Hash)
flatten_hash(v).map do|h_k, h_v|
h["#{k}.#{h_k}"] = h_v
end
else
h[k.to_s] = v
end
end
end
end