Class: SemanticLogger::Appender::Bugsnag

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

Overview

Send log messages to Bugsnag

Example:

SemanticLogger.add_appender(SemanticLogger::Appender::Bugsnag.new)

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, json_formatter, #level

Methods inherited from Base

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

Constructor Details

#initialize(options = {}, &block) ⇒ Bugsnag

Create Appender

Parameters

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

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.


25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/semantic_logger/appender/bugsnag.rb', line 25

def initialize(options = {}, &block)
  options  = {level: options} unless options.is_a?(Hash)
  @options  = options.dup
  level     = @options.delete(:level) || :error
  filter    = @options.delete(:filter)

  raise 'Bugsnag only supports :info, :warn, or :error log levels' unless [:info, :warn, :error].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, &block)
end

Instance Method Details

#default_formatterObject

Returns [Hash] of parameters to send to Bugsnag.



39
40
41
42
43
44
45
46
47
# File 'lib/semantic_logger/appender/bugsnag.rb', line 39

def default_formatter
  Proc.new do |log|
    h            = log.to_h
    h[:severity] = log_level(log)
    h.delete(:time)
    h.delete(:exception)
    h
  end
end

#log(log) ⇒ Object

Send an error notification to Bugsnag



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/semantic_logger/appender/bugsnag.rb', line 50

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, self))
  true
end