lib/semantic_logger/appender/bugsnag.rb in semantic_logger-4.7.2 vs lib/semantic_logger/appender/bugsnag.rb in semantic_logger-4.7.3
- old
+ new
@@ -38,45 +38,58 @@
super(level: level, **args, &block)
end
# Returns [Hash] of parameters to send to Bugsnag.
def call(log, logger)
- h = SemanticLogger::Formatters::Raw.new.call(log, logger)
- h[:severity] = log_level(log)
- h.delete(:message) if h[:exception] && (h[:message] == h[:exception][:message])
- h.delete(:time)
- h.delete(:exception)
- h
+ hash = SemanticLogger::Formatters::Raw.new.call(log, logger)
+ hash.delete(:message) if hash[:exception] && (hash[:message] == hash[:exception][:message])
+ hash.delete(:time)
+ hash.delete(:level_index)
+ hash.delete(:exception)
+ hash[:file] = "#{hash[:file]}:#{hash.delete(:line)}" if hash.key?(:file)
+ hash
end
- # Send an error notification to Bugsnag
def log(log)
# Ignore logs coming from Bugsnag itself
return false if log.name == "Bugsnag"
# Send error messages as Runtime exceptions
- exception =
- if log.exception
- # Manually constructed Exception, without a backtrace.
- log.exception.set_backtrace(log.backtrace) if !log.exception.backtrace && log.backtrace
- 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(exception, formatter.call(log, self))
+ exception = extract_exception(log)
+ hash = formatter.call(log, self)
+ bugsnag_notify(exception, hash, log_level(log.level))
true
end
private
+ def bugsnag_notify(exception, hash, level)
+ if ::Bugsnag::VERSION.to_i >= 6
+ ::Bugsnag.notify(exception) do |report|
+ report.severity = level
+ hash.each_pair { |key, value| report.add_tab(key, value) }
+ end
+ else
+ hash[:severity] = level
+ ::Bugsnag.notify(exception, hash)
+ end
+ end
+
+ def extract_exception(log)
+ if log.exception
+ # Manually constructed Exception, without a backtrace.
+ log.exception.set_backtrace(log.backtrace) if !log.exception.backtrace && log.backtrace
+ return log.exception
+ end
+
+ error = RuntimeError.new(log.message)
+ error.set_backtrace(log.backtrace) if log.backtrace
+ error
+ end
+
# Bugsnag supports: error, warning or info
- def log_level(log)
- case log.level
+ def log_level(level)
+ case level
when :error, :fatal
"error"
when :warn
"warning"
else