Class: Ougai::Formatters::Readable

Inherits:
Base
  • Object
show all
Defined in:
lib/ougai/formatters/readable.rb

Overview

A human readble formatter with awesome_print

Instance Attribute Summary collapse

Attributes inherited from Base

#app_name, #hostname, #trace_max_lines

Instance Method Summary collapse

Methods inherited from Base

#serialize_exc, #serialize_trace

Constructor Details

#initialize(opts = {}) ⇒ Readable

Returns a new instance of Readable.



12
13
14
15
16
17
18
# File 'lib/ougai/formatters/readable.rb', line 12

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

Instance Attribute Details

#excluded_fieldsArray<String, Symbol>

The fields excluded from all logs.

Returns:

  • (Array<String, Symbol>)

    the current value of excluded_fields



9
10
11
# File 'lib/ougai/formatters/readable.rb', line 9

def excluded_fields
  @excluded_fields
end

#plainBoolean

Whether log should be plain not colorized (by default this is off).

Returns:

  • (Boolean)

    the current value of plain



9
10
11
# File 'lib/ougai/formatters/readable.rb', line 9

def plain
  @plain
end

#trace_indentFixnum

The indent space size (by default this is 4).

Returns:

  • (Fixnum)

    the current value of trace_indent



9
10
11
# File 'lib/ougai/formatters/readable.rb', line 9

def trace_indent
  @trace_indent
end

Instance Method Details

#call(severity, time, progname, data) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/ougai/formatters/readable.rb', line 20

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

#colored_level(severity) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/ougai/formatters/readable.rb', line 34

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

#create_err_str(data) ⇒ Object



54
55
56
57
58
59
60
# File 'lib/ougai/formatters/readable.rb', line 54

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