Sha256: f0884609d8e1345398d0fe1be85e62233cb3a496f9d6b68d5460892218702346

Contents?: true

Size: 1.95 KB

Versions: 1

Compression:

Stored size: 1.95 KB

Contents

require 'ougai/formatters/base'

module Ougai
  module Formatters
    # A human readble formatter with awesome_print
    # @attr [Boolean] plain Whether log should be plain not colorized (by default this is off).
    # @attr [Fixnum] trace_indent The indent space size (by default this is 4).
    # @attr [Array<String, Symbol>] excluded_fields The fields excluded from all logs.
    class Readable < Base
      attr_accessor :plain, :trace_indent, :excluded_fields

      def initialize(opts = {})
        super(opts[:app_name], opts[:hostname])
        @trace_indent = opts[:trace_indent] || 4
        @plain = opts[:plain] || false
        @excluded_fields = opts[:excluded_fields] || []
        load_awesome_print
      end

      def call(severity, time, progname, data)
        msg = data.delete(:msg)
        level = @plain ? severity : colored_level(severity)
        strs = ["[#{time.iso8601(3)}] #{level}: #{msg}"]
        if err_str = create_err_str(data)
          strs.push(err_str)
        end
        @excluded_fields.each { |f| data.delete(f) }
        unless data.empty?
          strs.push(data.ai({ plain: @plain }))
        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

      def create_err_str(data)
        return nil unless data.key?(:err)
        err = data.delete(:err)
        err_str = "  #{err[:name]} (#{err[:message]}):"
        err_str += "\n    " + err[:stack] if err.key?(:stack)
        err_str
      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-1.4.4 lib/ougai/formatters/readable.rb