lib/semantic_logger/appender/bugsnag.rb in semantic_logger-2.16.0 vs lib/semantic_logger/appender/bugsnag.rb in semantic_logger-2.17.0
- old
+ new
@@ -46,39 +46,52 @@
class SemanticLogger::Appender::Bugsnag < SemanticLogger::Appender::Base
# Allow the level for this appender to be overwritten
# Default: :error
# Note: Not recommended to set the log level to :info, :debug, or :trace as that would flood Bugsnag with Error notices
def initialize(level = :error, &block)
+ # Replace the Bugsnag logger so that we can identify its log messages and not forward them to Bugsnag
+ Bugsnag.configure { |config| config.logger = SemanticLogger[Bugsnag] }
super(level, &block)
end
# Returns [Hash] of parameters to send to Bugsnag.
def default_formatter
proc do |log|
h = {severity: log_level(log), tags: log.tags, class: log.name}
+ h[:message] = log.message if log.exception
h.merge!(log.payload) if log.payload
h
end
end
# Send an error notification to Bugsnag
def log(log)
# Only log if level is warn, or error.
- # We don't want to send fatal as those are already captured by Bugsnag.
- return false if (level_index > (log.level_index || 0)) || !include_message?(log)
- return false if log.level == :fatal
- # Ignore logs coming from Bugsnag itself
- return false if log.message.to_s.include?(Bugsnag::LOG_PREFIX)
+ return false if (level_index > (log.level_index || 0)) ||
+ # We don't want to send fatal as those are already captured by Bugsnag.
+ (log.level == :fatal) ||
+ # Ignore logs coming from Bugsnag itself
+ (log.name == 'Bugsnag') ||
+ # Filtered out?
+ !include_message?(log)
+ # Send error messages as Runtime exceptions
+ exception =
+ if log.exception
+ log.exception
+ else
+ error = RuntimeError.new(log.message)
+ error.set_backtrace(log.backtrace) if log.backtrace
+ error
+ end
# For more documentation on the Bugsnag.notify method see:
# https://bugsnag.com/docs/notifiers/ruby#sending-handled-exceptions
- Bugsnag.notify(log.exception || RuntimeError.new(log.message), formatter.call(log))
+ Bugsnag.notify(exception, formatter.call(log))
true
end
private
def log_level(log)
- return 'warning' if log.level == :warn
- log.level.to_s
+ log.level == :warn ? 'warning' : log.level.to_s
end
end