Class: Synchrotron::Logger

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

Constant Summary collapse

LEVELS =
{
  :fatal  => 0,
  :error  => 1,
  :warn   => 2,
  :info   => 3,
  :debug  => 4,
  :insane => 5
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(level = :info, output = $stdout) ⇒ Logger

Returns a new instance of Logger.



15
16
17
18
# File 'lib/synchrotron/logger.rb', line 15

def initialize(level = :info, output = $stdout)
  self.level  = level.to_sym
  self.output = output
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object

Raises:

  • (NoMethodError)


25
26
27
28
# File 'lib/synchrotron/logger.rb', line 25

def method_missing(name, *args)
  return log(name, *args) if LEVELS.key?(name)
  raise NoMethodError, "undefined method: #{name}"
end

Instance Attribute Details

#levelObject

Returns the value of attribute level.



4
5
6
# File 'lib/synchrotron/logger.rb', line 4

def level
  @level
end

#outputObject

Returns the value of attribute output.



4
5
6
# File 'lib/synchrotron/logger.rb', line 4

def output
  @output
end

Instance Method Details

#const_missing(name) ⇒ Object

Raises:

  • (NameError)


20
21
22
23
# File 'lib/synchrotron/logger.rb', line 20

def const_missing(name)
  return LEVELS[name] if LEVELS.key?(name)
  raise NameError, "uninitialized constant: #{name}"
end

#log(level, msg) ⇒ Object



35
36
37
38
39
40
41
42
# File 'lib/synchrotron/logger.rb', line 35

def log(level, msg)
  return true if LEVELS[level] > LEVELS[@level] || msg.nil? || msg.empty?
  @output.puts "[#{Time.new.strftime('%b %d %H:%M:%S')}] [#{level}] #{msg}"
  true

rescue => e
  false
end