Class: Insight::Logger

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

Constant Summary collapse

DEBUG =
0
INFO =
1
WARN =
2
ERROR =
3
FATAL =
4
UNKNOWN =
5

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(level, path) ⇒ Logger

Returns a new instance of Logger.



3
4
5
6
7
# File 'lib/insight/logger.rb', line 3

def initialize(level, path)
  @level = level
  @log_path = path
  @logfile = nil
end

Instance Attribute Details

#levelObject

Returns the value of attribute level.



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

def level
  @level
end

Instance Method Details

#debugObject



37
# File 'lib/insight/logger.rb', line 37

def debug;   log(DEBUG,   yield) end

#errorObject



40
# File 'lib/insight/logger.rb', line 40

def error;   log(ERROR,   yield) end

#fatalObject



41
# File 'lib/insight/logger.rb', line 41

def fatal;   log(FATAL,   yield) end

#infoObject



38
# File 'lib/insight/logger.rb', line 38

def info;    log(INFO,    yield) end

#log(severity, message) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/insight/logger.rb', line 18

def log(severity, message)
  message = message.inspect unless String === message
  return unless severity >= @level

  if defined? Rails and
    Rails.respond_to? :logger
    not Rails.logger.nil?
    Rails.logger.add(severity, "[Insight]: " + message)
  end

  logfile.puts(message)
end

#logfileObject



31
32
33
34
35
# File 'lib/insight/logger.rb', line 31

def logfile
  @logfile ||= File::open(@log_path, "a+")
rescue
  $stderr
end

#unknownObject



42
# File 'lib/insight/logger.rb', line 42

def unknown; log(UNKNOWN, yield) end

#warnObject



39
# File 'lib/insight/logger.rb', line 39

def warn;    log(WARN,    yield) end