Class: Tracee::Preprocessors::Formatter

Inherits:
Base
  • Object
show all
Defined in:
lib/tracee/preprocessors/formatter.rb

Constant Summary collapse

COLORED_LEVELS =
{
  'debug' => 'DEBUG'.white,
  'info' => 'INFO'.light_cyan,
  'warn' => 'WARN'.light_magenta,
  'error' => 'ERROR'.light_yellow,
  'fatal' => 'FATAL'.light_red,
  'unknown' => 'UNKNOWN'.light_black
}.freeze
UPCASE_LEVELS =
Tracee::Logger::LEVEL_NAMES.map {|name| [name, name.upcase]}.to_h.freeze
TEMPLATES =
{
  tracee: {
    summary: "%{datetime} %{level} [%{caller}]: %{message}",
    datetime: "%T.%3N",
    level: COLORED_LEVELS,
    caller: "#{'%{file}:%{line}'.white} #{':%{method}'.light_red}"
  },
  
  logger_formatter: {
    summary: "%{level_letter}, [%{datetime} #%{pid}] %{level} -- %{progname}: %{message}",
    datetime: "%FT%T.%6N",
    level: UPCASE_LEVELS
  },
  
  plain: "%{message}",
  
  empty: ""
}.freeze
TEMPLATE_KEYS =
%w{datetime level level_letter pid progname caller message}.freeze
CALLER_KEYS =
%W{path file line method}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#halt!

Constructor Details

#initialize(params_or_key) ⇒ Formatter

available template keys : datetime, level, level_letter, pid, thread_id, progname, caller, message available caller keys : path, file, line, method params : {

  summary: <string containing available template keys as interpolation marks>,
  datetime: <format available to DateTime#strftime>,   # optional
  level: {<severity level name> => <label string>, ... },   # optional
  caller: <string containing available caller keys as interpolation marks>   # required if summary refers caller
}


48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/tracee/preprocessors/formatter.rb', line 48

def initialize(params_or_key)
  if params_or_key.is_a? Symbol
    params = TEMPLATES[params_or_key]
  end
  if params.is_a? String
    params = {summary: params}
  end
  
  unless params.is_a? Hash
    raise TypeError, 'params must be a Hash or a reference to one of Tracee::Formatters::Template::TEMPLATES'
  end
  
  @summary, @caller, @datetime, @level = params.values_at(:summary, :caller, :datetime, :level).map &:freeze
  @references = TEMPLATE_KEYS.select {|key| @summary["%{#{key}}"]}.to_set
end

Instance Attribute Details

#callerObject (readonly)

Returns the value of attribute caller.



37
38
39
# File 'lib/tracee/preprocessors/formatter.rb', line 37

def caller
  @caller
end

#datetimeObject (readonly)

Returns the value of attribute datetime.



37
38
39
# File 'lib/tracee/preprocessors/formatter.rb', line 37

def datetime
  @datetime
end

#levelObject (readonly)

Returns the value of attribute level.



37
38
39
# File 'lib/tracee/preprocessors/formatter.rb', line 37

def level
  @level
end

#summaryObject (readonly)

Returns the value of attribute summary.



37
38
39
# File 'lib/tracee/preprocessors/formatter.rb', line 37

def summary
  @summary
end

Instance Method Details

#call(msg_level, datetime, progname, msg, caller_slice = []) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/tracee/preprocessors/formatter.rb', line 65

def call(msg_level, datetime, progname, msg, caller_slice=[])
  result = @summary.dup
  
  if @references.include? 'datetime'
    result.sub! '%{datetime}', datetime.strftime(@datetime || '%FT%T%Z')
  end
  
  if @references.include? 'level' or @references.include? 'level_letter'
    level = @level[msg_level] || msg_level
    result.sub! '%{level}', level
    result.sub! '%{level_letter}', level[0]
  end
  
  if @references.include? 'pid'
    result.sub! '%{pid}', Process.pid.to_s
  end
  
  if @references.include? 'thread_id'
    result.sub! '%{thread_id}', Thread.current.object_id
  end
  
  if @references.include? 'progname'
    result.sub! '%{progname}', progname
  end
  
  if @references.include? 'caller'
    caller_slice = caller_slice.reverse.map {|line|
      path, file, line, is_block, block_level, method = line.match(CALLER_RE)[1..-1]
      block_level ||= is_block && '1'
      method = "#{method} {#{block_level}}" if block_level
      @caller % {path: path, file: file, line: line, method: method}
    } * ' -> '
    result.sub! '%{caller}', caller_slice
  end
  
  if @references.include? 'message'
    if msg.nil?
      msg = "\b\b"
    elsif !msg.is_a?(String)
      msg = msg.inspect
    end
    result.sub! '%{message}', msg
  end
  
  return result + "\n"
end

#inspectObject



118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/tracee/preprocessors/formatter.rb', line 118

def inspect
  summary = @summary.dup
  if @datetime
    summary.sub!('%{datetime}', DateTime.parse('2000-10-20 11:22:33.123456789').strftime(@datetime))
  end
  if @level
    summary.sub!('%{level}', "{#{@level.values*', '}}")
    summary.sub!('%{level_letter}', "{#{@level.values.map {|w| w[0]}*', '}}")
  end
  if @caller
    summary.sub!('%{caller}', @caller.to_s)
  end
  %{#{to_s.chop} "#{summary}">}
end

#should_process_caller?Boolean

Returns:

  • (Boolean)


113
114
115
# File 'lib/tracee/preprocessors/formatter.rb', line 113

def should_process_caller?
  @references.include? 'caller'
end