Class: Flexo::Logger

Inherits:
Object
  • Object
show all
Defined in:
lib/flexo/logger.rb

Overview

Having a little log of what’s going on might not be such a bad idea. This provides methods for outputting information to a logfile

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeLogger

Open a logfile for writing. We want to open the logger first, so instead of reading values from the config when initializing, we set them to defaults, and then, after the config has been loaded, override them.



16
17
18
19
20
# File 'lib/flexo/logger.rb', line 16

def initialize
  @logfile  = File.expand_path('~/flexo.log')
  @loglevel = 3
  @handle   = STDERR
end

Instance Attribute Details

#logfileObject

Returns the value of attribute logfile.



8
9
10
# File 'lib/flexo/logger.rb', line 8

def logfile
  @logfile
end

#loglevelObject

Returns the value of attribute loglevel.



9
10
11
# File 'lib/flexo/logger.rb', line 9

def loglevel
  @loglevel
end

Instance Method Details

#const_to_text(i) ⇒ Object

Quick and dirty hack to find the textual representation of a log level based on the numeric value



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/flexo/logger.rb', line 36

def const_to_text(i)
  hit = ""

  Flexo::Constants.constants.each do |const|
    if Flexo::Constants.const_get(const) == i
      hit = const
    end
  end

  return hit.split('_')[-1]
end

#debug(line) ⇒ Object

Convenience function for debug-messages



62
63
64
# File 'lib/flexo/logger.rb', line 62

def debug(line)
  log(Flexo::Constants::LOG_DEBUG, line)
end

#debug2(line) ⇒ Object

Convenience for more-verbose-than-debug-messages



67
68
69
# File 'lib/flexo/logger.rb', line 67

def debug2(line)
  log(Flexo::Constants::LOG_DEBUG2, line)
end

#error(line) ⇒ Object

Convenience for errors



82
83
84
# File 'lib/flexo/logger.rb', line 82

def error(line)
  log(Flexo::Constants::LOG_ERROR, line)
end

#info(line) ⇒ Object

Convenience for information



72
73
74
# File 'lib/flexo/logger.rb', line 72

def info(line)
  log(Flexo::Constants::LOG_INFO, line)
end

#log(level, line) ⇒ Object

Writes a line into the log



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

def log(level, line)
  i     = level
  level = const_to_text(level)

  if(i <= @loglevel)
    Thread.new do 
      @handle.print "#{level.to_s.upcase} #{Time.now} #{line}\n"
      @handle.flush
    end
  end
end

#warn(line) ⇒ Object

Convenience for warnings



77
78
79
# File 'lib/flexo/logger.rb', line 77

def warn(line)
  log(Flexo::Constants::LOG_WARN, line)
end