Class: SemanticLogger::Appender::NewRelicLogs

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

Instance Attribute Summary

Attributes inherited from Subscriber

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

Attributes inherited from Base

#filter, #name

Class Method Summary collapse

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(formatter: SemanticLogger::Formatters::NewRelicLogs.new, **args, &block) ⇒ NewRelicLogs

Create Appender

Parameters

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

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: SemanticLogger::Formatters::NewRelicLogs

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.


43
44
45
# File 'lib/semantic_logger/appender/new_relic_logs.rb', line 43

def initialize(formatter: SemanticLogger::Formatters::NewRelicLogs.new, **args, &block)
  super
end

Class Method Details

.log_newrelic(json_message, level) ⇒ Object



63
64
65
# File 'lib/semantic_logger/appender/new_relic_logs.rb', line 63

def self.log_newrelic(json_message, level)
  ::NewRelic::Agent.agent.log_event_aggregator.record(json_message, level)
end

Instance Method Details

#log(log) ⇒ Object

Send an error notification to New Relic



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

def log(log)
  begin
    message = formatter.call(log, self) # Generate the structured log
    json_message = message.to_json      # Convert the log to JSON
    level = log.level.to_s.upcase       # Determine the log level
    self.class.log_newrelic(json_message, level)
  rescue JSON::GeneratorError => e
    warn("Failed to serialize log message to JSON: #{e.message}")
    warn("Problematic data: #{message.inspect}")
  rescue StandardError => e
    warn("Unexpected error while logging to New Relic: #{e.message}")
  end
  true
end