Sha256: f8f8a7758d1480c7cc9dbb869c00dcc1d650cab2d3f8e4aa86b789fcb1e93532

Contents?: true

Size: 1.18 KB

Versions: 2

Compression:

Stored size: 1.18 KB

Contents

require 'logger'

module LicenseFinder
  class Logger
    MODE_QUIET = :quiet
    MODE_INFO = :info
    MODE_DEBUG = :debug

    attr_reader :mode

    def initialize(options = {})
      @system_logger = ::Logger.new(STDOUT)
      @system_logger.formatter = proc do |_, _, _, msg|
        "#{msg}\n"
      end

      self.mode = options[:mode] || MODE_INFO
    end

    [MODE_INFO, MODE_DEBUG].each do |level|
      define_method level do |prefix, string, options = {}|
        msg = format('%s: %s', prefix, colorize(string, options[:color]))
        log(msg, level)
      end
    end

    private

    attr_reader :system_logger

    def colorize(string, color)
      case color
      when :red
        "\e[31m#{string}\e[0m"
      when :green
        "\e[32m#{string}\e[0m"
      else
        string
      end
    end

    def mode=(v)
      @mode = v

      return if quiet?
      level = @mode.equal?(MODE_DEBUG) ? ::Logger::DEBUG : ::Logger::INFO
      system_logger.level = level
    end

    def log(msg, method)
      return if quiet?
      system_logger.send(method, msg)
    end

    def debug?
      @mode.equal?(MODE_DEBUG)
    end

    def quiet?
      @mode.equal?(MODE_QUIET)
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
license_finder-4.0.2 lib/license_finder/logger.rb
license_finder-4.0.1 lib/license_finder/logger.rb