Class: Betterlog::Log::LegacyEventFormatter
- Inherits:
-
ActiveSupport::Logger::Formatter
- Object
- ActiveSupport::Logger::Formatter
- Betterlog::Log::LegacyEventFormatter
- 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.
Instance Method Summary collapse
-
#call(severity, timestamp, program, message) ⇒ String
Processes a log message using the legacy formatting approach.
-
#emitter ⇒ String
Returns the emitter identifier string for legacy log events.
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.
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, , program, ) if cc.log.legacy_supported if .blank? = '' elsif !Log::Event.is?() m = .to_s m = Term::ANSIColor.uncolor(m) m = m.sub(/\s+$/, '') = .utc.iso8601(3) event = Log::Event.new( emitter: emitter, 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 = JSON.generate(event) end end rescue => e Betterlog::Log.logger.error(e) ensure = .to_s # Do not "message << ?\n" - A frozen string may be passed in .end_with?(?\n) or = "#{}\n" return end |
#emitter ⇒ String
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.
24 25 26 |
# File 'lib/betterlog/log/legacy_event_formatter.rb', line 24 def emitter 'legacy' end |