Sha256: cc2d5d490b97125a1a4156c7e4fd9774f10bb89cbffba3c446bcaeb7380560cb

Contents?: true

Size: 1.57 KB

Versions: 1

Compression:

Stored size: 1.57 KB

Contents

require 'itamae'
require 'logger'
require 'ansi/code'

module Itamae
  module Logger
    class Formatter
      attr_accessor :colored
      attr_accessor :depth

      INDENT_LENGTH = 3

      def initialize(*args)
        super

        @depth = 0
      end

      def call(severity, datetime, progname, msg)
        log = "%s : %s%s\n" % ["%5s" % severity, ' ' * INDENT_LENGTH * depth , msg2str(msg)]
        if colored
          color(log, severity)
        else
          log
        end
      end

      def indent
        @depth += 1
        yield
      ensure
        @depth -= 1
      end

      private
      def msg2str(msg)
        case msg
        when ::String
          msg
        when ::Exception
          "#{ msg.message } (#{ msg.class })\n" <<
          (msg.backtrace || []).join("\n")
        else
          msg.inspect
        end
      end

      def color(str, severity)
        color_code = case severity
                     when "INFO"
                       :green
                     when "WARN"
                       :magenta
                     when "ERROR"
                       :red
                     else
                       :clear
                     end
        ANSI.public_send(color_code) { str }
      end
    end

    class << self
      def logger
        @logger ||= ::Logger.new($stdout).tap do |logger|
          logger.formatter = Formatter.new
        end
      end

      def logger=(l)
        @logger = l
      end

      def method_missing(method, *args, &block)
        logger.public_send(method, *args, &block)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
itamae-1.0.0.beta34 lib/itamae/logger.rb