Class: ParticleLog

Inherits:
Object
  • Object
show all
Defined in:
lib/particlelog.rb,
lib/particlelog/version.rb

Constant Summary collapse

DEBUG =
0
IO =
1
INFO =
2
WARNING =
3
ERROR =
4
IMPORTANT =
5
CRITICAL =
7
C_BOLD =
"\x1b[1m"
C_RESET =
"\x1b[0m"
C_CRESET =
"\x1b[39m"
VERSION =
'0.1.1'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, level) ⇒ ParticleLog

Returns a new instance of ParticleLog.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/particlelog.rb', line 21

def initialize(name, level)
  @name  = name
  @level = level

  @level_table = [
    'DBG',
    ' IO',
    'INF',
    'WRN',
    'ERR',
    'IMP',
    'CRT'
  ]

  @color_table = [
    "\x1b[32m",
    "\x1b[36m",
    "\x1b[34m",
    "\x1b[33m",
    "\x1b[31m",
    "\x1b[37m",
    "\x1b[35m"
  ]
end

Instance Attribute Details

#color_tableObject (readonly)

Returns the value of attribute color_table.



18
19
20
# File 'lib/particlelog.rb', line 18

def color_table
  @color_table
end

#levelObject

Returns the value of attribute level.



19
20
21
# File 'lib/particlelog.rb', line 19

def level
  @level
end

#level_tableObject (readonly)

Returns the value of attribute level_table.



18
19
20
# File 'lib/particlelog.rb', line 18

def level_table
  @level_table
end

#nameObject (readonly)

Returns the value of attribute name.



18
19
20
# File 'lib/particlelog.rb', line 18

def name
  @name
end

Instance Method Details

#critical(message, **opts) ⇒ Object



58
# File 'lib/particlelog.rb', line 58

def critical  (message, **opts) write(message, **(opts.merge level:  CRITICAL)) end

#debug(message, **opts) ⇒ Object



52
# File 'lib/particlelog.rb', line 52

def debug     (message, **opts) write(message, **(opts.merge level:     DEBUG)) end

#error(message, **opts) ⇒ Object



56
# File 'lib/particlelog.rb', line 56

def error     (message, **opts) write(message, **(opts.merge level:     ERROR)) end

#important(message, **opts) ⇒ Object



57
# File 'lib/particlelog.rb', line 57

def important (message, **opts) write(message, **(opts.merge level: IMPORTANT)) end

#info(message, **opts) ⇒ Object



54
# File 'lib/particlelog.rb', line 54

def info      (message, **opts) write(message, **(opts.merge level:      INFO)) end

#inspectObject



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

def inspect
  "#<ParticleLog @name=#{@name.inspect} @level=#{@level_table[@level]}>"
end

#io(message, **opts) ⇒ Object



53
# File 'lib/particlelog.rb', line 53

def io        (message, **opts) write(message, **(opts.merge level:        IO)) end

#warning(message, **opts) ⇒ Object



55
# File 'lib/particlelog.rb', line 55

def warning   (message, **opts) write(message, **(opts.merge level:   WARNING)) end

#write(message, **opts) ⇒ Object



46
47
48
49
50
# File 'lib/particlelog.rb', line 46

def write(message, **opts)
  level   = opts[:level]   || @level
  c = @color_table[level]
  STDERR.print "#{C_BOLD+c}[#{C_CRESET+DateTime.now.strftime+C_BOLD+c} | #{C_CRESET}#{@level_table[level]+C_BOLD+c}] #{C_RESET+name+C_BOLD+c}> #{C_RESET+message}\n"
end