Sha256: cd84c448fe809658051205dea770562fcd1aff70878257903476c3a96972ee78

Contents?: true

Size: 1.26 KB

Versions: 1

Compression:

Stored size: 1.26 KB

Contents

require 'ougai/formatters/base'

module Ougai
  module Formatters
    class Readable < Base
      def initialize(app_name = nil, hostname = nil)
        super(app_name, hostname)
        @trace_indent = 4
        load_awesome_print
      end

      def call(severity, time, progname, data)
        msg = data.delete(:msg)
        strs = ["[#{time.iso8601(3)}] #{colored_level(severity)}: #{msg}"]
        if data.key?(:err)
          err = data.delete(:err)
          err_str = "  #{err[:name]} (#{err[:message]}):"
          err_str += "\n    " + err[:stack] if err.key?(:stack)
          strs.push(err_str)
        end
        unless data.empty?
          strs.push(data.ai)
        end
        strs.join("\n") + "\n"
      end

      def colored_level(severity)
        case severity
        when 'INFO'
          color = '0;36'
        when 'WARN'
          color = '0;33'
        when 'ERROR'
          color = '0;31'
        when 'FATAL'
          color = '0;35'
        else # DEBUG
          color = '0;37'
        end
        "\e[#{color}m#{severity}\e[0m"
      end

      private

      def load_awesome_print
        require 'awesome_print'
      rescue LoadError
        puts 'You must install the awesome_print gem to use this output.'
        raise
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ougai-0.7.1 lib/ougai/formatters/readable.rb