Class: SemanticLogger::Appender::Bugsnag

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

Overview

Send log messages to Bugsnag

Example:

SemanticLogger.add_appender(appender: :bugsnag)

Instance Attribute Summary

Attributes inherited from Subscriber

#application, #formatter, #host

Attributes inherited from Base

#filter, #name

Instance Method Summary collapse

Methods inherited from Subscriber

#close, #default_formatter, #flush, #level

Methods inherited from Base

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

Constructor Details

#initialize(options = {}, &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.


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

def initialize(options = {}, &block)
  # Backward compatibility
  options             = {level: options} unless options.is_a?(Hash)
  options             = options.dup
  options[:level]     = :error unless options.has_key?(:level)

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

Instance Method Details

#call(log, logger) ⇒ Object

Returns [Hash] of parameters to send to Bugsnag.



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

def call(log, logger)
  h            = log.to_h(host, application)
  h[:severity] = log_level(log)
  h.delete(:time)
  h.delete(:exception)
  h
end

#log(log) ⇒ Object

Send an error notification to Bugsnag



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

def log(log)
  return false unless should_log?(log)
  # Ignore logs coming from Bugsnag itself
  return false if log.name == 'Bugsnag'

  # 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