Class: Logsaber::Formatter

Inherits:
Object
  • Object
show all
Defined in:
lib/logsaber/formatter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(color = nil) ⇒ Formatter

Returns a new instance of Formatter.



5
6
7
# File 'lib/logsaber/formatter.rb', line 5

def initialize color = nil
  @color = color
end

Instance Attribute Details

#colorObject

Returns the value of attribute color.



8
9
10
# File 'lib/logsaber/formatter.rb', line 8

def color
  @color
end

#logObject

Returns the value of attribute log.



8
9
10
# File 'lib/logsaber/formatter.rb', line 8

def log
  @log
end

#time_formatObject

Returns the value of attribute time_format.



8
9
10
# File 'lib/logsaber/formatter.rb', line 8

def time_format
  @time_format
end

Instance Method Details

#appnameObject



64
65
66
# File 'lib/logsaber/formatter.rb', line 64

def appname
  log.appname
end

#appname?Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/logsaber/formatter.rb', line 60

def appname?
  !!appname
end

#color!Object



19
20
21
22
# File 'lib/logsaber/formatter.rb', line 19

def color!
  require 'logsaber/simple_color'
  @color = SimpleColor.new
end

#color?Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/logsaber/formatter.rb', line 24

def color?
  !!@color
end

#format(severity, contents) ⇒ Object



28
29
30
31
32
33
34
35
36
37
# File 'lib/logsaber/formatter.rb', line 28

def format severity, contents
  index = log.class::SEVERITY_LEVELS.index(severity)
  text  = layout severity, contents

  if color? then
    color.colorize index, text
  else
    text
  end
end

#layout(severity, contents) ⇒ Object



39
40
41
# File 'lib/logsaber/formatter.rb', line 39

def layout severity, contents
  %(#{timestamp} [#{severity_info severity}] #{process_info} | #{view contents})
end

#process_infoObject



55
56
57
58
# File 'lib/logsaber/formatter.rb', line 55

def process_info
  pid = Process.pid.to_s
  appname? ? "#{appname}:#{pid}" : pid
end

#set_log(new_log) ⇒ Object



10
11
12
13
# File 'lib/logsaber/formatter.rb', line 10

def set_log new_log
  self.log = new_log
  self
end

#severity_info(severity) ⇒ Object



51
52
53
# File 'lib/logsaber/formatter.rb', line 51

def severity_info severity
  severity.to_s.upcase.rjust 5
end

#timestampObject



47
48
49
# File 'lib/logsaber/formatter.rb', line 47

def timestamp
  Time.now.strftime time_format
end

#view(contents) ⇒ Object



43
44
45
# File 'lib/logsaber/formatter.rb', line 43

def view contents
  "#{contents[:label]} : #{contents[:info].join ' | '}"
end