Sha256: 0e1486076dcb77aaa6b6e6ea2e362e5103e1f4907cb52f7705d18eba7e669633

Contents?: true

Size: 1.24 KB

Versions: 12

Compression:

Stored size: 1.24 KB

Contents

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

module Itamae
  module Logger
    class Formatter
      def call(severity, datetime, progname, msg)
        "[%s] %s : %s\n" % [format_datetime(datetime), color("%5s" % severity, severity), msg2str(msg)]
      end

      private
      def format_datetime(time)
        time.strftime("%Y-%m-%dT%H:%M:%S%:z")
      end

      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 "ERROR"
                       :red
                     else
                       :clear
                     end
        ANSI.public_send(color_code) { str }
      end
    end

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

    def self.logger=(l)
      @logger = l
    end

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

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
itamae-1.0.0.beta13 lib/itamae/logger.rb
itamae-1.0.0.beta12 lib/itamae/logger.rb
itamae-1.0.0.beta11 lib/itamae/logger.rb
itamae-1.0.0.beta10 lib/itamae/logger.rb
itamae-1.0.0.beta9 lib/itamae/logger.rb
itamae-1.0.0.beta8 lib/itamae/logger.rb
itamae-1.0.0.beta7 lib/itamae/logger.rb
itamae-1.0.0.beta6 lib/itamae/logger.rb
itamae-1.0.0.beta5 lib/itamae/logger.rb
itamae-1.0.0.beta4 lib/itamae/logger.rb
itamae-1.0.0.beta3 lib/itamae/logger.rb
itamae-1.0.0.beta2 lib/itamae/logger.rb