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



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

def debug;   log(DEBUG,   yield) end

#errorObject



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

def error;   log(ERROR,   yield) end

#fatalObject



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

def fatal;   log(FATAL,   yield) end

#infoObject



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

def info;    log(INFO,    yield) end

#log(severity, message) ⇒ Object



18
19
20
21
22
23
24
25
26
27
# 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) and not Rails.logger.nil?
    Rails.logger.add(severity, "[Insight]: " + message)
  end

  logfile.puts(message)
end

#logfileObject



29
30
31
32
33
# File 'lib/insight/logger.rb', line 29

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

#unknownObject



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

def unknown; log(UNKNOWN, yield) end

#warnObject



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

def warn;    log(WARN,    yield) end