Sha256: d76a8b4caa06ef1b35c5cc3a6433f639775401df1b16b5d4cc836ed2e3c26569

Contents?: true

Size: 1.11 KB

Versions: 8

Compression:

Stored size: 1.11 KB

Contents

# coding: utf-8
# frozen_string_literal: true

module Stealth
  class Logger

    COLORS = ::Hash[
      black:   30,
      red:     31,
      green:   32,
      yellow:  33,
      blue:    34,
      magenta: 35,
      cyan:    36,
      gray:    37,
    ].freeze

    def self.color_code(code)
      COLORS.fetch(code) { raise(ArgumentError, "Color #{code} not supported.") }
    end

    def self.colorize(input, color:)
      "\e[#{color_code(color)}m#{input}\e[0m"
    end

    def self.log(topic:, message:)
      unless ENV['STEALTH_ENV'] == 'test'
        puts "#{print_topic(topic)} #{message}"
      end
    end

    def self.print_topic(topic)
      topic_string = "[#{topic}]"

      case topic.to_sym
      when :session
        colorize(topic_string, color: :green)
      when :previous_session
        colorize(topic_string, color: :yellow)
      when :facebook, :twilio
        colorize(topic_string, color: :blue)
      when :catch_all
        colorize(topic_string, color: :red)
      else
        colorize(topic_string, color: :gray)
      end
    end

    class << self
      alias_method :l, :log
    end

  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
stealth-1.1.0.rc1 lib/stealth/logger.rb
stealth-1.0.4 lib/stealth/logger.rb
stealth-1.0.3 lib/stealth/logger.rb
stealth-1.0.2 lib/stealth/logger.rb
stealth-1.0.1 lib/stealth/logger.rb
stealth-1.0.0 lib/stealth/logger.rb
stealth-1.0.0.rc1 lib/stealth/logger.rb
stealth-1.0.0.pre2 lib/stealth/logger.rb