Class: NewRelic::Agent::Logging::DecoratingFormatter

Inherits:
Logger::Formatter
  • Object
show all
Defined in:
lib/new_relic/agent/logging.rb

Overview

This class can be used as the formatter for an existing logger. It decorates log messages with trace and entity metadata, and formats each log messages as a JSON object.

It can be added to a Rails application like this:

require 'newrelic_rpm'

Rails.application.configure do
  config.log_formatter = ::NewRelic::Agent::Logging::DecoratingFormatter.new
end

Constant Summary collapse

MESSAGE_ELEMENT =
'message'
LOG_LEVEL_ELEMENT =
'log_level'
PROG_NAME_ELEMENT =
'prog_name'
ELEMENTS =
['app_name', 'entity_type', 'hostname', 'entity_guid', 'trace_id', 'span_id', MESSAGE_ELEMENT,
LOG_LEVEL_ELEMENT, PROG_NAME_ELEMENT].freeze
TIMESTAMP_KEY =
'timestamp'
MESSAGE_KEY =
'message'
LOG_LEVEL_KEY =
'log.level'
LOG_NAME_KEY =
'logger.name'
NEWLINE =
"\n"
QUOTE =
'"'
COLON =
':'
COMMA =
','
CLOSING_BRACE =
'}'
REPLACEMENT_CHAR =
''

Instance Method Summary collapse

Constructor Details

#initializeDecoratingFormatter

Returns a new instance of DecoratingFormatter.



42
43
44
45
46
# File 'lib/new_relic/agent/logging.rb', line 42

def initialize
  Agent.config.register_callback(:app_name) do
    @app_name = nil
  end
end

Instance Method Details

#call(severity, time, progname, msg) ⇒ Object



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

def call(severity, time, progname, msg)
  message = +'{'
  ELEMENTS.each do |element|
    args = case element
           when MESSAGE_ELEMENT then [message, msg]
           when LOG_LEVEL_ELEMENT then [message, severity]
           when PROG_NAME_ELEMENT then [message, progname]
           else [message]
    end

    send("add_#{element}", *args)
  end
  message << COMMA
  message << QUOTE << TIMESTAMP_KEY << QUOTE << COLON << (time.to_f * 1000).round.to_s
  message << CLOSING_BRACE << NEWLINE
end

#clear_tags!Object



65
66
67
# File 'lib/new_relic/agent/logging.rb', line 65

def clear_tags!
  # No-op; just avoiding issues with act-fluent-logger-rails
end