Class: SemanticLogger::Appender::Sentry

Inherits:
Subscriber
  • Object
show all
Defined in:
lib/semantic_logger/appender/sentry.rb

Constant Summary

Constants inherited from Subscriber

Subscriber::MESSAGE_ERRORS

Instance Attribute Summary

Attributes inherited from Subscriber

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

Instance Method Summary collapse

Methods inherited from Subscriber

#batch_all_or_nothing?, #batch_by_default?, #batch_refusal, #close, #console_output?, #console_stream, #fails_any_batch?, #flush, #inspect, #inspect_details, #message_error?, #not_sent?, #rejected_entry, #should_log?, #wrote_nothing?

Constructor Details

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

Create Appender

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

formatter: [Object|Proc|Symbol|Hash]
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.

host: [String]
Name of this host to appear in log messages.
Default: SemanticLogger.host

application: [String]
Name of this application to appear in log messages.
Default: SemanticLogger.application


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

def initialize(level: :error, **args, &block)
  Deprecation.warn(
    "SemanticLogger::Appender::Sentry (appender: :sentry) is deprecated and will be removed in v6. " \
    "It depends on the end-of-life sentry-raven gem. Use appender: :sentry_ruby instead."
  )
  # Replace the Sentry Raven logger so that we can identify its log messages and not forward them to Sentry
  Raven.configure { |config| config.logger = SemanticLogger[Raven] }
  super
end

Instance Method Details

#log(log) ⇒ Object

Send an error notification to sentry



52
53
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/sentry.rb', line 52

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

  context      = format_log(log)
  user         = context.delete(:user)
  tags         = context.delete(:tags)
  attrs        = {
    level: context.delete(:level),
    extra: context
  }
  attrs[:user] = user if user
  attrs[:tags] = tags if tags
  if log.exception
    context.delete(:exception)
    Raven.capture_exception(log.exception, attrs)
  else
    attrs[:extra][:backtrace] = log.backtrace if log.backtrace
    Raven.capture_message(context[:message], attrs)
  end
  true
end