Class: Ariel::Log

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/ariel/log.rb

Overview

Very simple Log class. By default outputs to stdout and ignored messages below :info level. Should probably get rid of the usage of Singleton as it’s used very little, with the classes eigenclass/singleton class used mostly for the same purpose. Use Log.set_level to lower/raise the logging level.

Constant Summary collapse

SEVERITY =
{:debug=>0, :info=>1, :warn=>2, :error=>3}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeLog

Level defaults to :debug if $DEBUG is set and :info if not.



15
16
17
18
19
20
21
22
# File 'lib/ariel/log.rb', line 15

def initialize
  self.class.output_to_stdout
  if $DEBUG
    self.class.set_level :debug
  else
    self.class.set_level :info
  end
end

Class Method Details

.current_levelObject



38
39
40
# File 'lib/ariel/log.rb', line 38

def current_level
  @log_level
end

.log(message, level) ⇒ Object

Not intended to be used directly, preferred to use the methods corresponding to different serverity levels.



53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/ariel/log.rb', line 53

def log(message, level)
  if SEVERITY[@log_level] <= SEVERITY[level]
    message = "#{level}: #{message}"
    if @output==:file
      File.open('debug.log', 'ab') {|f| f.puts message }
    elsif @output==:stdout
      puts message
    end
    return message          
  end
  return nil
end

.output_to_fileObject

Sends all output to a file called debug.log in the current directory.



47
48
49
# File 'lib/ariel/log.rb', line 47

def output_to_file
  @output=:file
end

.output_to_stdoutObject



42
43
44
# File 'lib/ariel/log.rb', line 42

def output_to_stdout
  @output=:stdout
end

.set_level(level) ⇒ Object

Set the log level to the given key from the SEVERITY constant.



30
31
32
33
34
35
36
# File 'lib/ariel/log.rb', line 30

def set_level(level)
  if SEVERITY.has_key? level
    @log_level=level
  else
    raise ArgumentError, "Invalid log level given"
  end
end