Module: Logging

Included in:
SrcML, Vcs2Json::Git
Defined in:
lib/vcs2json/logger.rb

Overview

enable logging in classes through ‘include Logging’

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.configure_logger_for(classname) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/vcs2json/logger.rb', line 17

def configure_logger_for(classname)
  begin
    logger = Logger.new(@logger_location,'daily')
  rescue Errno::EACCES
    tmp = Tempfile.new('vcs2json')
    $stderr.puts "Tried to create a logging file here: '#{@logger_location}', but didn't have permission, defaulting to OS temp firectory: #{tmp.path}"
    logger = Logger.new(tmp,'daily')
  end
  logger.progname = classname
  logger.level = const_get('Logger::'+@logger_level.upcase)
  logger
end

.logger_for(classname) ⇒ Object



13
14
15
# File 'lib/vcs2json/logger.rb', line 13

def logger_for(classname)
  @loggers[classname] ||= configure_logger_for(classname)
end

.set_level(level) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/vcs2json/logger.rb', line 34

def set_level(level)
  possible_levels = %w(debug info warn error info)
  if !level.nil?
    if !level.empty?
      if possible_levels.include?(level)
        STDERR.puts "Logging level has been set to '#{level}' for output to #{@logger_location}"
        @loggers.each {|l| l.level = const_get('Logger::'+level.upcase)}
        @logger_level = level
      else
        STDERR.puts "Unable to set logger level to #{level}, possible values are #{possible_levels}. Defaulting to 'info'."
      end
    end
  end
end

.set_location(path) ⇒ Object



30
31
32
# File 'lib/vcs2json/logger.rb', line 30

def set_location(path)
  @logger_location = path
end

Instance Method Details

#loggerObject



3
4
5
# File 'lib/vcs2json/logger.rb', line 3

def logger
  @logger ||= Logging.logger_for(self.class.name)
end