Sha256: dda2f8a3b5a9b4297735a325f5526bccba047f1a0a30048c0d317ac69c962192

Contents?: true

Size: 1.61 KB

Versions: 1

Compression:

Stored size: 1.61 KB

Contents

require 'term/ansicolor'

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

    def emitter
      'legacy'
    end

    def call(severity, timestamp, program, message)
      message = super.to_s
      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
      # Do not "message << ?\n" - A frozen string may be passed in
      message.end_with?(?\n) or message = "#{message}\n"
      return message
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
betterlog-0.20.1 lib/betterlog/log_event_formatter.rb