Sha256: 0e56a2171e4ab3f9600bb555707976df66546de9533aa63ab3483da1eb5a3df4

Contents?: true

Size: 1.72 KB

Versions: 2

Compression:

Stored size: 1.72 KB

Contents

require 'term/ansicolor'

module Betterlog
  class Log
    class LegacyEventFormatter < ::ActiveSupport::Logger::Formatter
      include ActiveSupport::TaggedLogging::Formatter
      include ComplexConfig::Provider::Shortcuts

      def emitter
        'legacy'
      end

      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
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
betterlog-2.0.4 lib/betterlog/log/legacy_event_formatter.rb
betterlog-2.0.3 lib/betterlog/log/legacy_event_formatter.rb