Class: VGH::Logging

Inherits:
Object
  • Object
show all
Defined in:
lib/vgh/logging.rb

Overview

Description:

This class logs messages if logging is enabled from the command line options. The default location of the log files is /var/log/vgh.log.

This class uses the Ruby Logger standard library.

Usage:

log = Logging.new.log
log.info "This is an info message"

Instance Method Summary collapse

Constructor Details

#initializeLogging

Check log file existence



36
37
38
39
# File 'lib/vgh/logging.rb', line 36

def initialize
  defaults
  validate_log_directory
end

Instance Method Details

#defaultsObject

Defaults



30
31
32
33
# File 'lib/vgh/logging.rb', line 30

def defaults
  @path  = '/var/log/vgh.log'
  @level = Logger::INFO
end

#logObject

Global, memoized, lazy initialized instance of a logger



57
58
59
60
61
62
63
# File 'lib/vgh/logging.rb', line 57

def log
  # Logger
  @log ||= Logger.new(log_file)
  @log.level = @level
  @log.datetime_format = "%Y-%m-%d %H:%M "  # simplify time output
  @log
end

#log_fileObject

Opens the log file



48
49
50
51
52
53
54
# File 'lib/vgh/logging.rb', line 48

def log_file
  begin
    File.open(@path, File::WRONLY | File::APPEND | File::CREAT)
  rescue
    STDOUT
  end
end

#validate_log_directoryObject

Creates a log directory and file if it does not already exist



42
43
44
45
# File 'lib/vgh/logging.rb', line 42

def validate_log_directory
  dir = File.dirname(@path)
  Dir.mkdir(dir) unless File.exists?(dir)
end