class Af::Logging::Configurator

Public Class Methods

new() click to toggle source
# File lib/fiksu-af/logging/configurator.rb, line 17
def initialize
  @@singleton = self
  Log4r::Configurator.custom_levels(:DEBUG, :DEBUG_FINE, :DEBUG_MEDIUM, :DEBUG_GROSS, :DETAIL, :INFO, :WARN, :ALARM, :ERROR, :FATAL)
end
singleton() click to toggle source

Return the single allowable instance of this class, if the class has been instantiated

# File lib/fiksu-af/logging/configurator.rb, line 13
def self.singleton
  return @@singleton ||= new
end

Public Instance Methods

configurate() click to toggle source
# File lib/fiksu-af/logging/configurator.rb, line 115
def configurate
  if (@@log_console || @@log_ignore_configuration) && !@@log_configurate
    Log4r::Logger.root.outputters << Log4r::Outputter.stdout
  else
    logging_load_configuration
    if logging_configuration_looks_bogus
      Log4r::Logger.root.outputters << Log4r::Outputter.stdout
    end
  end

  if @@log_levels
    set_logger_levels(@@log_levels)
  end

  if @@log_dump_configuration
    puts "Log configuration search path:"
    puts " " + @@log_configuration_search_path.join("\n ")
    puts "Log configuration files:"
    puts " " + @@log_configuration_files.join("\n ")
    puts "Logging Names: #{Log4r::LNAMES.join(', ')}"
    puts "Yaml section names:"
    puts " " + @@log_configuration_section_names.join("\n ")
    loggers = []
    Log4r::Logger.each do |logger_name, logger|
      loggers << logger_name
    end
    puts "Loggers:"
    puts "global: #{Log4r::LNAMES[Log4r::Logger.global.level]}"
    puts "root: #{Log4r::LNAMES[Log4r::Logger['root'].level]} [#{Log4r::Logger['root'].outputters.map{|o| o.name}.join(', ')}]"
    loggers.sort.reject{|logger_name| ["root", "global"].include? logger_name}.each do |logger_name|
      puts "#{' ' * logger_name.split('::').length}#{logger_name}: #{Log4r::LNAMES[Log4r::Logger[logger_name].level]} [#{Log4r::Logger[logger_name].outputters.map{|o| o.name}.join(', ')}]"
    end
    exit 0
  end
end
logger(logger_name = :default) click to toggle source

Returns the logger with the provided name, instantiating it if needed.

Arguments

* logger_name - logger to return, defaults to ":default"
# File lib/fiksu-af/logging/configurator.rb, line 48
def logger(logger_name = :default)
  # Coerce the logger_name if needed.
  logger_name = ::Af::Application.singleton.af_name if logger_name == :default
  # Check with Log4r to see if there is a logger by this name.
  # If Log4r doesn't have a logger by this name, make one with Af defaults.
  return Log4r::Logger[logger_name] || Log4r::Logger.new(logger_name)
end
logging_configuration_looks_bogus() click to toggle source

TODO AK: What is purpose of this method?

# File lib/fiksu-af/logging/configurator.rb, line 86
def logging_configuration_looks_bogus
  return Log4r::LNAMES.length == 1
end
logging_load_configuration() click to toggle source

Load all of the Log4r yaml configuration files.

# File lib/fiksu-af/logging/configurator.rb, line 74
def logging_load_configuration
  files = []
  @@log_configuration_files.each do |configuration_file|
    @@log_configuration_search_path.each do |path|
      pathname = Pathname.new(path) + configuration_file
      files << pathname.to_s if pathname.file?
    end
  end
  logging_load_configuration_files(files, @@log_configuration_section_names)
end
logging_load_configuration_files(files, yaml_sections) click to toggle source

Load the provided yaml Log4r configuration files.

Arguments

* files - array of file names with full paths (??)
* yaml_sections - ???
# File lib/fiksu-af/logging/configurator.rb, line 61
def logging_load_configuration_files(files, yaml_sections)
  begin
    Log4r::YamlConfigurator.load_yaml_files(files, yaml_sections)
  rescue StandardError => e
    puts "error while parsing log configuration files: #{e.message}"
    puts "continuing without your configuration"
    puts e.backtrace.join("\n")
    return false
  end
  return true
end
parse_and_set_logger_levels(logger_info) click to toggle source

Parses and sets the provided logger levels.

Argument

* logger_info - value indicating default log level, or JSON string
  of logger names to logger levels, i.e. "{'foo' => 'INFO'}.
# File lib/fiksu-af/logging/configurator.rb, line 95
def parse_and_set_logger_levels(logger_info)
  log_level_hash = JSON.parse(logger_info) rescue {:default => parse_log_level(logger_info)}
  set_logger_levels(log_level_hash)
end
parse_log_level(logger_level) click to toggle source

Parse and return the provided log level, which can be an integer, string integer or string constant. Returns all loging levels if value cannot be parsed.

Arguments

* logger_level - log level to be parsed
# File lib/fiksu-af/logging/configurator.rb, line 28
def parse_log_level(logger_level)
  if logger_level.is_a? Integer
    logger_level_value = logger_level
  elsif logger_level.is_a? String
    if logger_level[0] =~ /[0-9]/
      logger_level_value = logger_level.to_i
    else
      logger_level_value = logger_level.constantize rescue nil
      logger_level_value = "Log4r::#{logger_level.upcase}".constantize rescue nil unless logger_level_value
    end
  else
    logger_level_value = Log4r::ALL
  end
  return logger_level_value
end
set_logger_levels(log_level_hash) click to toggle source

Sets the logger levels the provided hash. It supports the following formats for logger levels: 1, “1”, “INFO”, “Log4r::INFO”.

Arguments

* log_level_hash - hash of logger names to logger levels,
  i.e. { :foo => 'INFO' }
# File lib/fiksu-af/logging/configurator.rb, line 106
def set_logger_levels(log_level_hash)
  log_level_hash.each do |logger_name, logger_level|
    logger_name = :default if logger_name == "default"
    logger_level_value = parse_log_level(logger_level)
    l = logger(logger_name)
    l.level = logger_level_value
  end
end