Class: SemanticLogger::Appender::Bugsnag

Inherits:
Subscriber show all
Defined in:
lib/semantic_logger/appender/bugsnag.rb

Instance Attribute Summary

Attributes inherited from Subscriber

#application, #environment, #formatter, #host, #logger, #metrics

Attributes inherited from Base

#filter, #name

Instance Method Summary collapse

Methods inherited from Subscriber

#close, #console_output?, #default_formatter, #flush, #level, #should_log?

Methods inherited from Base

#backtrace, #fast_tag, #level, #level=, #measure, #named_tags, #pop_tags, #push_tags, #should_log?, #silence, #tagged, #tags

Constructor Details

#initialize(level: :error, **args, &block) ⇒ Bugsnag

Create Bugsnag Error / Exception Appender

Parameters

level: [:trace | :debug | :info | :warn | :error | :fatal]
  Override the log level for this appender.
  Default: :error

formatter: [Object|Proc]
  An instance of a class that implements #call, or a Proc to be used to format
  the output from this appender
  Default: Use the built-in formatter (See: #call)

filter: [Regexp|Proc]
  RegExp: Only include log messages where the class name matches the supplied.
  regular expression. All other messages will be ignored.
  Proc: Only include log messages where the supplied Proc returns true
        The Proc must return true or false.


32
33
34
35
36
37
38
39
# File 'lib/semantic_logger/appender/bugsnag.rb', line 32

def initialize(level: :error, **args, &block)
  raise "Bugsnag only supports :info, :warn, or :error log levels" unless %i[info warn error fatal].include?(level)

  # 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: level, **args, &block)
end

Instance Method Details

#call(log, logger) ⇒ Object

Returns [Hash] of parameters to send to Bugsnag.



42
43
44
45
46
47
48
49
50
# File 'lib/semantic_logger/appender/bugsnag.rb', line 42

def call(log, logger)
  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

#log(log) ⇒ Object



52
53
54
55
56
57
58
59
60
61
# File 'lib/semantic_logger/appender/bugsnag.rb', line 52

def log(log)
  # Ignore logs coming from Bugsnag itself
  return false if log.name == "Bugsnag"

  # Send error messages as Runtime exceptions
  exception = extract_exception(log)
  hash      = formatter.call(log, self)
  bugsnag_notify(exception, hash, log_level(log.level))
  true
end