Sha256: 0ffa8445537a10217906ca5d4c7ffe4d8257f9509df588c5dfe1ea752bfb872b

Contents?: true

Size: 1.21 KB

Versions: 24

Compression:

Stored size: 1.21 KB

Contents

# frozen_string_literal: true

require 'logger'

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

    attr_reader :mode

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

      self.mode = 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=(verbose)
      @mode = verbose

      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

24 entries across 24 versions & 1 rubygems

Version Path
license_finder-6.5.0 lib/license_finder/logger.rb
license_finder-6.4.0 lib/license_finder/logger.rb
license_finder-6.3.0 lib/license_finder/logger.rb
license_finder-6.2.0 lib/license_finder/logger.rb
license_finder-6.1.2 lib/license_finder/logger.rb
license_finder-6.1.0 lib/license_finder/logger.rb
license_finder-6.0.0 lib/license_finder/logger.rb
license_finder-5.11.1 lib/license_finder/logger.rb
license_finder-5.11.0 lib/license_finder/logger.rb
license_finder-5.10.2 lib/license_finder/logger.rb
license_finder-5.10.1 lib/license_finder/logger.rb
license_finder-5.10.0 lib/license_finder/logger.rb
license_finder-5.9.2 lib/license_finder/logger.rb
license_finder-5.9.1 lib/license_finder/logger.rb
license_finder-5.9.0 lib/license_finder/logger.rb
license_finder-5.8.0 lib/license_finder/logger.rb
license_finder-5.7.1 lib/license_finder/logger.rb
license_finder-5.7.0 lib/license_finder/logger.rb
license_finder-5.6.2 lib/license_finder/logger.rb
license_finder-5.6.1 lib/license_finder/logger.rb