Class: UsageTracker::Log

Inherits:
Object
  • Object
show all
Defined in:
lib/usage_tracker/log.rb

Constant Summary collapse

Levels =
{
    :fatal => Logger::FATAL, 
    :erorr => Logger::ERROR, 
    :warn  => Logger::WARN, 
    :info  => Logger::INFO, 
    :debug => Logger::DEBUG 
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeLog

Returns a new instance of Log.



20
21
22
# File 'lib/usage_tracker/log.rb', line 20

def initialize
  open
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



14
15
16
# File 'lib/usage_tracker/log.rb', line 14

def path
  @path
end

Instance Method Details

#closeObject



41
42
43
44
45
46
47
# File 'lib/usage_tracker/log.rb', line 41

def close
  return unless @logger

  @logger.info 'Log closed'
  @logger.close
  @logger = nil
end

#level=(level) ⇒ Object



54
55
56
57
58
# File 'lib/usage_tracker/log.rb', line 54

def level=(level)
  level = level.to_sym 
  raise "Invalid log level" unless Levels.keys.include?(level)
  @logger.level = Levels[level]
end

#openObject



32
33
34
35
36
37
38
39
# File 'lib/usage_tracker/log.rb', line 32

def open
  @logger           = Logger.new(path.to_s)
  @logger.formatter = Logger::Formatter.new
  @logger.info 'Log opened'

rescue
  raise Error, "Cannot open log file #{path}"
end

#rotateObject



49
50
51
52
# File 'lib/usage_tracker/log.rb', line 49

def rotate
  close
  open
end