Sha256: a4a7b9b2e2b673b27d9367f2551f2183ae20453872263a2ad4facd2021f52a9a

Contents?: true

Size: 1.11 KB

Versions: 2

Compression:

Stored size: 1.11 KB

Contents

require 'logger'

module Filum
  class Logger < ::Logger

    def initialize(*args)
      super
      self.formatter = Filum::Logger::Formatter.new
    end

    def context_id=(context_id)
      Thread.current[:context_id] = context_id
    end

    class Formatter < Logger::Formatter
      def call(severity, timestamp, progname, msg)
        "#{timestamp} thread_id-#{Thread.current.object_id} [#{formatted_context_id}] #{severity} | #{formatted_calling_file_and_line} | #{msg}\n"
      end

      private
      def formatted_context_id
        context_id.ljust(Filum.config.context_id_length)
      end

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

      def formatted_calling_file_and_line
        filename_length = Filum.config.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 calling_code
        caller[4]
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
filum-1.0.0 lib/filum/logger.rb
filum-0.1.0 lib/filum/logger.rb