Sha256: 9d6e97295ecba743eefacb11c8f39f430bdeba2ef5bbca9665452f3cc546de3b

Contents?: true

Size: 1.33 KB

Versions: 3

Compression:

Stored size: 1.33 KB

Contents

require 'logger'
require 'ansi/code'

module Minarai
  class Logger
    class Formatter
      def call(severity, _datetime, _progname, msg)
        @severity = severity
        colorize "%{severity} : %{msg}\n" % {
          severity: severity,
          msg: msg
        }
      end

      private

      def colorize(log)
        ANSI.public_send(color) { log }
      end

      def color
        case @severity
        when 'INFO'
          :green
        when 'WARN'
          :magenta
        when 'ERROR', 'FATAL'
          :red
        end
      end
    end

    class << self
      METHODS = %i(warn info error fatal)

      METHODS.each do |method_name|
        define_method(method_name) do |msg|
          if msg.is_a?(Array)
            msg.each do |m|
              logger.public_send(method_name, m)
            end
          else
            logger.public_send(method_name, msg)
          end
        end
      end
      alias_method :skip, :info
      alias_method :done, :info
      alias_method :fail, :warn

      def logger
        @logger ||= create_logger
      end
      alias_method :new, :logger

      private

      def logger_device
        @logger_device || $stdout
      end

      def create_logger
        ::Logger.new(logger_device).tap do |logger|
          logger.formatter = Formatter.new
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
minarai-0.0.3 lib/minarai/logger.rb
minarai-0.0.2 lib/minarai/logger.rb
minarai-0.0.1 lib/minarai/logger.rb