Sha256: 51022d099bfaef56c4f130c8a052f5e93484f61bd80625351849780bcbe3f275

Contents?: true

Size: 1.51 KB

Versions: 9

Compression:

Stored size: 1.51 KB

Contents

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

module Itamae
  module Logger
    class Formatter
      attr_accessor :colored

      def call(severity, datetime, progname, msg)

        severity = if colored
                     color("%5s" % severity, severity)
                   else
                     "%5s" % severity
                   end

        "[%s] %s : %s\n" % [format_datetime(datetime), 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 "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

9 entries across 9 versions & 1 rubygems

Version Path
itamae-1.0.0.beta32 lib/itamae/logger.rb
itamae-1.0.0.beta31 lib/itamae/logger.rb
itamae-1.0.0.beta30 lib/itamae/logger.rb
itamae-1.0.0.beta29 lib/itamae/logger.rb
itamae-1.0.0.beta28 lib/itamae/logger.rb
itamae-1.0.0.beta27 lib/itamae/logger.rb
itamae-1.0.0.beta26 lib/itamae/logger.rb
itamae-1.0.0.beta25 lib/itamae/logger.rb
itamae-1.0.0.beta24 lib/itamae/logger.rb