Class: SemanticLogger::Appender::Bugsnag

Inherits:
Base
  • Object
show all
Defined in:
lib/semantic_logger/appender/bugsnag.rb

Instance Attribute Summary

Attributes inherited from Base

#formatter

Attributes inherited from Base

#filter, #name

Instance Method Summary collapse

Methods inherited from Base

colorized_formatter, #flush, #level

Methods inherited from Base

#benchmark, default_level, default_level=, #level, #level=, #payload, #pop_tags, #push_tags, #silence, #tagged, #tags, #with_payload

Constructor Details

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

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


50
51
52
53
54
# File 'lib/semantic_logger/appender/bugsnag.rb', line 50

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

Instance Method Details

#default_formatterObject

Returns [Hash] of parameters to send to Bugsnag.



57
58
59
60
61
62
63
64
# File 'lib/semantic_logger/appender/bugsnag.rb', line 57

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

#log(log) ⇒ Object

Send an error notification to Bugsnag



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/semantic_logger/appender/bugsnag.rb', line 67

def log(log)
  # Only log if level is warn, or error.
  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(exception, formatter.call(log))
  true
end