Class: Betterlog::Log::LegacyEventFormatter

Inherits:
ActiveSupport::Logger::Formatter
  • Object
show all
Includes:
ActiveSupport::TaggedLogging::Formatter, ComplexConfig::Provider::Shortcuts
Defined in:
lib/betterlog/log/legacy_event_formatter.rb

Overview

Formats log messages for legacy Rails logging compatibility.

This class provides a formatter that integrates with Rails’ legacy logging system, converting standard log messages into structured JSON events while preserving the original formatting behavior for backward compatibility.

See Also:

  • Event
  • ActiveSupport::Logger::Formatter

Instance Method Summary collapse

Instance Method Details

#call(severity, timestamp, program, message) ⇒ String

Processes a log message using the legacy formatting approach.

This method handles the conversion of standard log messages into structured JSON events when legacy logging support is enabled. It extracts relevant information from the input message, such as backtraces and location data, and formats it according to the legacy event structure.

Parameters:

  • severity (String)

    the severity level of the log message

  • timestamp (Time)

    the time when the log entry was created

  • program (String)

    the name of the program generating the log

  • message (String)

    the raw log message content

Returns:

  • (String)

    the formatted log message, either as a JSON event or the original message if it’s not suitable for conversion



43
44
45
46
47
48
49
50
51
52
53
54
55
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
# File 'lib/betterlog/log/legacy_event_formatter.rb', line 43

def call(severity, timestamp, program, message)
  if cc.log.legacy_supported
    if message.blank?
      message = ''
    elsif !Log::Event.is?(message)

      m = message.to_s
      m = Term::ANSIColor.uncolor(m)
      m = m.sub(/\s+$/, '')

      timestamp = timestamp.utc.iso8601(3)
      event = Log::Event.new(
        emitter:    emitter,
        timestamp:  timestamp,
        message:    m,
        severity:   severity.to_s.downcase,
        # tags:       current_tags,
      )
      backtrace = m.scan(/^\s*(?:[^:]+):(?:\d+).*$/)
      if backtrace.size > 1
        event[:backtrace] = backtrace.map { |b| b.sub(/\s+$/, '') }
        event[:message] = "#{backtrace.first}\n"
      end
      if l = caller_locations.reverse_each.each_cons(2).find { |c, n|
        n.absolute_path =~ /\/lib\/ruby\/.*?\/logger\.rb/ and break c
      }
        then
        event[:location] = [ l.absolute_path, l.lineno ] * ?:
      end
      program and event[:program] = program
      message = JSON.generate(event)
    end
  end
rescue => e
  Betterlog::Log.logger.error(e)
ensure
  message = message.to_s
  # Do not "message << ?\n" - A frozen string may be passed in
  message.end_with?(?\n) or message = "#{message}\n"
  return message
end

#emitterString

Returns the emitter identifier string for legacy log events.

This method provides a constant string value that represents the emitter type for logs formatted using the legacy event formatter.

Returns:

  • (String)

    the string ‘legacy’ indicating the legacy emitter type



24
25
26
# File 'lib/betterlog/log/legacy_event_formatter.rb', line 24

def emitter
  'legacy'
end