Class: SemanticLogger::Appender::NewRelic

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

Instance Attribute Summary

Attributes inherited from Subscriber

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

Attributes inherited from Base

#filter, #name

Instance Method Summary collapse

Methods inherited from Subscriber

#close, #console_output?, #default_formatter, #flush, #level, #should_log?

Methods inherited from Base

#backtrace, #fast_tag, #level, #level=, #measure, #named_tags, #pop_tags, #push_tags, #should_log?, #silence, #tagged, #tags

Constructor Details

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

Create 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.


34
35
36
# File 'lib/semantic_logger/appender/new_relic.rb', line 34

def initialize(level: :error, **args, &block)
  super(level: level, **args, &block)
end

Instance Method Details

#call(log, logger) ⇒ Object

Returns [Hash] of parameters to send to New Relic.



39
40
41
42
43
44
# File 'lib/semantic_logger/appender/new_relic.rb', line 39

def call(log, logger)
  h = SemanticLogger::Formatters::Raw.new.call(log, logger)
  h.delete(:time)
  h.delete(:exception)
  {metric: log.metric, custom_params: h}
end

#log(log) ⇒ Object

Send an error notification to New Relic



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/semantic_logger/appender/new_relic.rb', line 47

def log(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 NewRelic::Agent.notice_error method see:
  # http://rubydoc.info/github/newrelic/rpm/NewRelic/Agent#notice_error-instance_method
  # and https://docs.newrelic.com/docs/ruby/ruby-agent-api
  ::NewRelic::Agent.notice_error(exception, formatter.call(log, self))
  true
end