Sha256: bf3cb3083c0349c52076068eb51efee19814fcac6a959dcf237f82ffbafc13ac

Contents?: true

Size: 1009 Bytes

Versions: 2

Compression:

Stored size: 1009 Bytes

Contents

module Paraduct
  class ColoredLabelLogger < ::Logger
    def initialize(label_name, logdev = STDOUT)
      super(logdev)
      color = Paraduct::ColoredLabelLogger.next_color
      @formatter = Formatter.new(label_name, color)
    end

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

    class Formatter
      def initialize(label_name, color)
        @label = "[#{label_name.to_s.colorize(color)}]"
      end

      def call(severity, datetime, progname, message)
        return "" if message.blank?

        content = ""
        message.each_line do |line|
          content << "#{datetime} #{@label} #{line.strip}\n"
        end
        content
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
paraduct-1.0.1 lib/paraduct/colored_label_logger.rb
paraduct-1.0.0 lib/paraduct/colored_label_logger.rb