Class: SemanticLogger::Appender::SentryRuby

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

Instance Attribute Summary

Attributes inherited from Subscriber

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

Instance Method Summary collapse

Methods inherited from Subscriber

#batch_by_default?, #close, #console_output?, #console_stream, #flush, #level, #should_log?

Constructor Details

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

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


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

def initialize(level: :error, **args, &block)
  # Replace the Sentry Ruby logger so that we can identify its log
  # messages and not forward them to Sentry
  unless ::Sentry.initialized?
    ::Sentry.init do |config|
      logger = SemanticLogger[::Sentry]
      config.respond_to?(:sdk_logger=) ? config.sdk_logger = logger : config.logger = logger
    end
  end
  super
end

Instance Method Details

#log(log) ⇒ Object

Send an error notification to sentry



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/semantic_logger/appender/sentry_ruby.rb', line 53

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

  context = formatter.call(log, self)
  payload = context.delete(:payload) || {}
  named_tags = context[:named_tags] || {}
  transaction_name = named_tags.delete(:transaction_name)

  user = extract_user!(named_tags, payload)
  tags = extract_tags!(context)

  fingerprint = payload.delete(:fingerprint)

  ::Sentry.with_scope do |scope|
    scope.set_user(user) if user
    scope.set_level(context.delete(:level)) if context[:level]
    scope.set_fingerprint(fingerprint) if fingerprint
    scope.set_transaction_name(transaction_name) if transaction_name
    scope.set_tags(tags)
    scope.set_extras(context)
    scope.set_extras(payload)

    if log.exception
      ::Sentry.capture_exception(log.exception)
    elsif log.backtrace
      ::Sentry.capture_message(context[:message], backtrace: log.backtrace)
    else
      ::Sentry.capture_message(context[:message])
    end
  end

  true
end