Sha256: 1327d7d5af0d2d281d12e47b8a4e3fd760e9cdcb1f4767886bd9fd6d39c468f3

Contents?: true

Size: 1.12 KB

Versions: 4

Compression:

Stored size: 1.12 KB

Contents

require 'logger'

module Filum
  class LogFormatter < Logger::Formatter
    def initialize(options = {})
      @context_id_length = options.fetch(:context_id_length, 6)
      @filename_length = options.fetch(:filename_length, 20)
    end

    def call(severity, timestamp, progname, msg)
      "#{timestamp} #{formatted_thread_id} [#{formatted_context_id}] #{severity.to_s.ljust(5)} | #{formatted_calling_file_and_line} | #{msg}\n"
    end

    private
    def formatted_context_id
      context_id.ljust(@context_id_length)
    end

    def context_id
      Thread.current[:context_id].to_s
    end

    def formatted_calling_file_and_line
      filename_length = @filename_length
      truncated_filename_length = filename_length - 3

      _, file, line = calling_code.match(/([\w\.]+)\:(\d+)\:in /).to_a
      file = "#{file[0,truncated_filename_length]}..." if file.length >= filename_length
      "#{file}:#{line.ljust(3)}".ljust(filename_length + 4)
    end

    def formatted_thread_id
      "t-#{thread_id}".ljust(12)
    end

    def calling_code
      caller[5]
    end

    def thread_id
      Thread.current.object_id
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
filum-2.2.1 lib/filum/log_formatter.rb
filum-2.2.0 lib/filum/log_formatter.rb
filum-2.1.0 lib/filum/log_formatter.rb
filum-2.0.0 lib/filum/log_formatter.rb