Class: Rakie::Log

Inherits:
Object
  • Object
show all
Defined in:
lib/rakie/log.rb

Constant Summary collapse

LEVEL_INFO =
0
LEVEL_ERROR =
1
LEVEL_DEBUG =
2

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeLog

Returns a new instance of Log.



11
12
13
14
# File 'lib/rakie/log.rb', line 11

def initialize
  @level = Rakie.current_mode == Rakie::MODE_DEV ? LEVEL_DEBUG : LEVEL_ERROR
  @out = STDOUT
end

Instance Attribute Details

#levelObject

Returns the value of attribute level.



3
4
5
# File 'lib/rakie/log.rb', line 3

def level
  @level
end

#outObject

Returns the value of attribute out.



3
4
5
# File 'lib/rakie/log.rb', line 3

def out
  @out
end

Class Method Details

.debug(message, who = nil) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/rakie/log.rb', line 75

def self.debug(message, who=nil)
  unless self.instance.level_debug?
    return
  end

  if who
    message = "#{who}: #{message}"
  end

  level = self.instance.level_text(LEVEL_DEBUG)
  self.instance.out.print("[#{Time.now.to_s}][#{level}] #{message}\n")
end

.error(message, who = nil) ⇒ Object



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

def self.error(message, who=nil)
  unless self.instance.level_error?
    return
  end

  if who
    message = "#{who}: #{message}"
  end

  level = self.instance.level_text(LEVEL_ERROR)
  self.instance.out.write("[#{Time.now.to_s}][#{level}] #{message}\n")
end

.info(message, who = nil) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/rakie/log.rb', line 49

def self.info(message, who=nil)
  unless self.instance.level_info?
    return
  end

  if who
    message = "#{who}: #{message}"
  end

  level = self.instance.level_text(LEVEL_INFO)
  self.instance.out.write("[#{Time.now.to_s}][#{level}] #{message}\n")
end

.instanceObject



45
46
47
# File 'lib/rakie/log.rb', line 45

def self.instance
  @instance ||= Log.new
end

.levelObject



88
89
90
# File 'lib/rakie/log.rb', line 88

def self.level
  self.instance.level
end

.level=(level) ⇒ Object



92
93
94
# File 'lib/rakie/log.rb', line 92

def self.level=(level)
  self.instance.level = level
end

Instance Method Details

#level_debug?Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/rakie/log.rb', line 41

def level_debug?
  @level >= LEVEL_DEBUG
end

#level_error?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/rakie/log.rb', line 37

def level_error?
  @level >= LEVEL_ERROR
end

#level_info?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/rakie/log.rb', line 33

def level_info?
  @level >= LEVEL_INFO
end

#level_text(level = nil) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/rakie/log.rb', line 16

def level_text(level=nil)
  unless level
    level = @level
  end

  case level
  when LEVEL_INFO
    return "INFO"

  when LEVEL_ERROR
    return "ERROR"

  when LEVEL_DEBUG
    return "DEBUG"
  end
end