Class: Logsaber::Formatter
- Inherits:
-
Object
- Object
- Logsaber::Formatter
- Defined in:
- lib/logsaber/formatter.rb
Instance Attribute Summary collapse
-
#color ⇒ Object
Returns the value of attribute color.
-
#log ⇒ Object
Returns the value of attribute log.
-
#time_format ⇒ Object
Returns the value of attribute time_format.
Instance Method Summary collapse
- #appname ⇒ Object
- #appname? ⇒ Boolean
- #color! ⇒ Object
- #color? ⇒ Boolean
- #format(severity, contents) ⇒ Object
-
#initialize(color = nil) ⇒ Formatter
constructor
A new instance of Formatter.
- #layout(severity, contents) ⇒ Object
- #process_info ⇒ Object
- #set_log(new_log) ⇒ Object
- #severity_info(severity) ⇒ Object
- #timestamp ⇒ Object
- #view(contents) ⇒ Object
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
#color ⇒ Object
Returns the value of attribute color.
8 9 10 |
# File 'lib/logsaber/formatter.rb', line 8 def color @color end |
#log ⇒ Object
Returns the value of attribute log.
8 9 10 |
# File 'lib/logsaber/formatter.rb', line 8 def log @log end |
#time_format ⇒ Object
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
#appname ⇒ Object
64 65 66 |
# File 'lib/logsaber/formatter.rb', line 64 def appname log.appname end |
#appname? ⇒ 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
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 %(#{} [#{severity_info severity}] #{process_info} | #{view contents}) end |
#process_info ⇒ Object
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 |
#timestamp ⇒ Object
47 48 49 |
# File 'lib/logsaber/formatter.rb', line 47 def 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 |