Class: SemanticLogger::Appender::NewRelic

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

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

Methods inherited from Base

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

Constructor Details

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

Allow the level for this appender to be overwritten

Default: :error
Note: Not recommended to set the log level to :info, :debug, or :trace as that would flood NewRelic with Error notices


65
66
67
# File 'lib/semantic_logger/appender/new_relic.rb', line 65

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

Instance Method Details

#default_formatterObject

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



70
71
72
73
74
75
76
77
78
79
80
# File 'lib/semantic_logger/appender/new_relic.rb', line 70

def default_formatter
  Proc.new do |log|
    custom_params            = {thread_name: log.thread_name}
    custom_params[:duration] = "#{log.duration} ms" if log.duration
    custom_params[:payload]  = log.payload if log.payload
    custom_params[:tags]     = log.tags if log.tags && (log.tags.size > 0)
    custom_params[:message]  = log.message if log.exception

    {metric: log.metric, custom_params: custom_params}
  end
end

#log(log) ⇒ Object

Send an error notification to New Relic



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/semantic_logger/appender/new_relic.rb', line 83

def log(log)
  # Ensure minimum log level is met, and check filter
  return false if (level_index > (log.level_index || 0)) || !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 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))
  true
end