Class: ScoutApm::Logging::Loggers::Formatter

Inherits:
Logger::Formatter
  • Object
show all
Defined in:
lib/scout_apm/logging/loggers/formatter.rb

Overview

A simple JSON formatter which we can add a couple attributes to.

Constant Summary collapse

DATETIME_FORMAT =
'%Y-%m-%dT%H:%M:%S.%LZ'

Instance Method Summary collapse

Instance Method Details

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

rubocop:disable Metrics/AbcSize



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/scout_apm/logging/loggers/formatter.rb', line 15

def call(severity, time, progname, msg) # rubocop:disable Metrics/AbcSize
  attributes_to_log = {
    severity: severity,
    time: format_datetime(time),
    msg: msg2str(msg)
  }

  attributes_to_log[:progname] = progname if progname
  attributes_to_log['service.name'] = service_name

  attributes_to_log.merge!(scout_transaction_id)
  attributes_to_log.merge!(scout_layer)
  attributes_to_log.merge!(scout_context)
  # Naive local benchmarks show this takes around 200 microseconds. As such, we only apply it to WARN and above.
  attributes_to_log.merge!(local_log_location) if ::Logger::Severity.const_get(severity) >= ::Logger::Severity::WARN

  message = "#{attributes_to_log.to_json}\n"

  ScoutApm::Logging::Loggers::OpenTelemetry.logger_provider.logger(
    name: 'scout_apm',
    version: '0.1.0'
  ).on_emit(
    severity_text: severity,
    severity_number: ::Logger::Severity.const_get(severity),
    attributes: attributes_to_log,
    timestamp: time,
    body: msg,
    context: ::OpenTelemetry::Context.current
  )
  message
end