Class: SemanticLogger::Formatters::NewRelicLogs

Inherits:
Raw
  • Object
show all
Defined in:
lib/semantic_logger/formatters/new_relic_logs.rb

Overview

Formatter for reporting to NewRelic’s Logger

New Relic’s logs do not support custom attributes out of the box, and therefore these have to be put into a single JSON serialized string under the message key.

In particular the following fields of the log object are serialized under the message key that’s sent to NewRelic:

  • message

  • tags

  • named_tags

  • payload

  • metric

  • metric_amount

  • environment

  • application

New Relic Attributes not Supported

  • thread.id

  • class.name

  • method.name

Reference

Instance Attribute Summary

Attributes inherited from Raw

#hash, #time_key

Attributes inherited from Base

#filter, #name

Instance Method Summary collapse

Methods inherited from Raw

#application, #duration, #environment, #exception, #file_name_and_line, #host, #level, #message, #metric, #name, #named_tags, #payload, #pid, #tags, #thread_name, #time

Methods inherited from Base

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

Constructor Details

#initialize(**args) ⇒ NewRelicLogs

[View source]

49
50
51
52
53
54
# File 'lib/semantic_logger/formatters/new_relic_logs.rb', line 49

def initialize(**args)
  args.delete(:time_key)
  args.delete(:time_format)

  super(time_key: :timestamp, time_format: :ms, **args)
end

Instance Method Details

#call(log, logger) ⇒ Object

[View source]

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
87
88
89
90
91
92
93
94
95
# File 'lib/semantic_logger/formatters/new_relic_logs.rb', line 56

def call(log, logger)
  hash = super(log, logger)

  message = {
    message:    hash[:message].to_s,
    tags:       hash[:tags] || [],
    named_tags: hash[:named_tags] || {},

    **hash.slice(:metric, :metric_amount, :environment, :application, :payload)
  }

  message.merge!(duration: hash[:duration_ms]) if hash.key?(:duration_ms)
  message.merge!(duration_human: hash[:duration]) if hash.key?(:duration)

  result = {
    **,
    message:       message.to_json,
    timestamp:     hash[:timestamp].to_i,
    "log.level":   log.level.to_s.upcase,
    "logger.name": log.name,
    "thread.name": log.thread_name.to_s
  }

  if hash[:exception]
    result.merge!(
      "error.message": hash[:exception][:message],
      "error.class":   hash[:exception][:name],
      "error.stack":   hash[:exception][:stack_trace].join("\n")
    )
  end

  if hash[:file]
    result.merge!(
      "file.name":   hash[:file],
      "line.number": hash[:line].to_s
    )
  end

  result
end