Sha256: c08a7bd9d657168f025c5de5991acb74b00b178cbfc4605955bbc8ef9008a0ba

Contents?: true

Size: 1.26 KB

Versions: 19

Compression:

Stored size: 1.26 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"
      when :magenta
        "\e[35m#{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

19 entries across 19 versions & 2 rubygems

Version Path
license_finder-6.15.0 lib/license_finder/logger.rb
gitlab-license_finder-6.14.2.1 lib/license_finder/logger.rb
license_finder-6.14.2 lib/license_finder/logger.rb
license_finder-6.14.1 lib/license_finder/logger.rb
license_finder-6.13.0 lib/license_finder/logger.rb
license_finder-6.12.2 lib/license_finder/logger.rb
license_finder-6.12.1 lib/license_finder/logger.rb
license_finder-6.12.0 lib/license_finder/logger.rb
license_finder-6.11.0 lib/license_finder/logger.rb
license_finder-6.10.1 lib/license_finder/logger.rb
license_finder-6.10.0 lib/license_finder/logger.rb
license_finder-6.9.0 lib/license_finder/logger.rb
license_finder-6.8.2 lib/license_finder/logger.rb
license_finder-6.8.1 lib/license_finder/logger.rb
license_finder-6.8.0 lib/license_finder/logger.rb
license_finder-6.7.0 lib/license_finder/logger.rb
license_finder-6.6.2 lib/license_finder/logger.rb
license_finder-6.6.1 lib/license_finder/logger.rb
license_finder-6.6.0 lib/license_finder/logger.rb