Module: LogBot

Defined in:
lib/drakkon/lib/logbot.rb

Overview

Log Helper

Class Method Summary collapse

Class Method Details

.clock_timeObject

From Rack Time



83
84
85
# File 'lib/drakkon/lib/logbot.rb', line 83

def self.clock_time
  Process.clock_gettime(Process::CLOCK_MONOTONIC)
end

.color_level(level) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/drakkon/lib/logbot.rb', line 27

def self.color_level(level)
  case level
  when :debug
    level.to_s.upcase.pastel(:bright_cyan)
  when :info
    level.to_s.upcase.pastel(:cyan)
  when :warn
    level.to_s.upcase.pastel(:yellow)
  when :error
    level.to_s.upcase.pastel(:bright_red)
  when :fatal
    level.to_s.upcase.pastel(:red)
  end
end

.debug(name, message = nil, level = :debug) ⇒ Object



46
47
48
# File 'lib/drakkon/lib/logbot.rb', line 46

def self.debug(name, message = nil, level = :debug)
  output(name, message, level)
end

.do(name, message = nil, level = nil) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/drakkon/lib/logbot.rb', line 62

def self.do(name, message = nil, level = nil)
  began_at = clock_time

  level ||= :info
  result = yield

  duration = (clock_time - began_at).round(3)

  output(name, message, level, duration)

  result
end

.error(name, message = nil, level = :error) ⇒ Object



54
55
56
# File 'lib/drakkon/lib/logbot.rb', line 54

def self.error(name, message = nil, level = :error)
  output(name, message, level)
end

.fatal(name, message = nil, level = :fatal) ⇒ Object



58
59
60
# File 'lib/drakkon/lib/logbot.rb', line 58

def self.fatal(name, message = nil, level = :fatal)
  output(name, message, level)
end

.info(name, message = nil, level = :info) ⇒ Object



42
43
44
# File 'lib/drakkon/lib/logbot.rb', line 42

def self.info(name, message = nil, level = :info)
  output(name, message, level)
end

.measureObject



75
76
77
78
79
80
# File 'lib/drakkon/lib/logbot.rb', line 75

def self.measure
  began_at = clock_time
  result = yield
  duration = (clock_time - began_at).round(3)
  [result, duration]
end

.output(name, message, level, duration = nil) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/drakkon/lib/logbot.rb', line 8

def self.output(name, message, level, duration = nil)
  time = Time.now.strftime('%I:%M:%S').pastel(:bright_black)
  output = time
  output += "  #{color_level(level)}"
  output += " - #{name.to_s.pastel(:bright_blue)} "
  output += "(#{duration.round(2)}) ".pastel(:magenta) if duration

  output += ' '
  if message
    output += case message
              when String then message
              else
                message.ai(settings)
              end
  end

  puts output unless ENV['TESTING']
end

.settingsObject

For Amazing Print



4
5
6
# File 'lib/drakkon/lib/logbot.rb', line 4

def self.settings
  { ruby19_syntax: true, multiline: false }
end

.warn(name, message = nil, level = :warn) ⇒ Object



50
51
52
# File 'lib/drakkon/lib/logbot.rb', line 50

def self.warn(name, message = nil, level = :warn)
  output(name, message, level)
end