Sha256: 7f2381c6cda67236601880bf076a4e69667175414177fd4439423512c57328d3

Contents?: true

Size: 980 Bytes

Versions: 3

Compression:

Stored size: 980 Bytes

Contents

module Paraduct
  class ThreadLogger < ::Logger
    def initialize(logdev = STDOUT)
      super(logdev)
      color = Paraduct::ThreadLogger.next_color
      @label = "[#{object_id.to_s.colorize(color)}]"
      @formatter = ActiveSupport::Logger::SimpleFormatter.new
    end

    [:debug, :info, :warn, :error, :fatal].each do |severity|
      define_method "#{severity}_with_label" do |message|
        message.each_line do |line|
          send "#{severity}_without_label", "#{@label} #{line.strip}" unless line.blank?
        end
      end
      alias_method_chain severity, :label
    end

    COLORS = [
      :cyan,
      :yellow,
      :green,
      :magenta,
      :red,
      :blue,
      :light,
      :cyan,
      :light_yellow,
      :light_green,
      :light_magenta,
      :light_red,
      :light_blue,
    ]
    def self.next_color
      @@color_index ||= -1
      @@color_index = (@@color_index + 1) % COLORS.length
      COLORS[@@color_index]
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
paraduct-0.0.1.beta11 lib/paraduct/thread_logger.rb
paraduct-0.0.1.beta10 lib/paraduct/thread_logger.rb
paraduct-0.0.1.beta9 lib/paraduct/thread_logger.rb