Sha256: 9b8b69b9e6a76b0f833bab5771caea0be8ce3482b18f9d4c7951d1d6b3e25961

Contents?: true

Size: 1.1 KB

Versions: 35

Compression:

Stored size: 1.1 KB

Contents

require "singleton"
require "colorize"

module Bisu
  module Logger
    extend self

    def info(msg)
      log :info, msg
    end

    def warn(msg)
      log :warn, msg
    end

    def error(msg)
      log :error, msg
    end

    def clean_summary
      @levels.each { |k, _| @levels[k] = 0 }
    end

    def summary
      @levels
    end

    def print_summary
      if @levels[:warn] > 0 || @levels[:error] > 0
        info ""
        info "Finished with:"
        info "  #{@levels[:warn]} warnings" if @levels[:warn]  > 0
        info "  #{@levels[:error]} errors"  if @levels[:error] > 0
        info ""
      end
    end

    def silent_mode=(value)
      @silent_mode = value
    end

    private

    @levels = { info: 0, warn: 0, error: 0 }
    @silent_mode = false

    def log(level, msg)
      unless @levels.keys.include?(level)
        return log(:error, "Unknown log level: #{level}")
      end

      @levels[level] += 1

      msg = "[#{level.upcase}] #{msg}"
      msg = msg.yellow if level.eql?(:warn)
      msg = msg.red    if level.eql?(:error)

      puts msg unless @silent_mode
    end
  end
end

Version data entries

35 entries across 35 versions & 1 rubygems

Version Path
bisu-2.3.0 lib/bisu/logger.rb
bisu-2.2.0 lib/bisu/logger.rb
bisu-2.1.0 lib/bisu/logger.rb
bisu-2.0.0 lib/bisu/logger.rb
bisu-1.10.2 lib/bisu/logger.rb
bisu-1.10.1 lib/bisu/logger.rb
bisu-1.10.0 lib/bisu/logger.rb
bisu-1.9.0 lib/bisu/logger.rb
bisu-1.8.0 lib/bisu/logger.rb
bisu-1.7.3 lib/bisu/logger.rb
bisu-1.7.2 lib/bisu/logger.rb
bisu-1.7.1 lib/bisu/logger.rb
bisu-1.7.0 lib/bisu/logger.rb
bisu-1.6.0 lib/bisu/logger.rb
bisu-1.5.0 lib/bisu/logger.rb
bisu-1.4.7 lib/bisu/logger.rb
bisu-1.4.6 lib/bisu/logger.rb
bisu-1.4.5 lib/bisu/logger.rb
bisu-1.4.4 lib/bisu/logger.rb
bisu-1.4.3 lib/bisu/logger.rb