class Hyla::Logger2

Attributes

levels[R]
log[R]

Public Class Methods

new(mode, log_yml_file, dirname = nil, logname = nil, level = nil, tracer = nil) click to toggle source
# File lib/hyla/logger2.rb, line 12
def initialize(mode, log_yml_file, dirname = nil, logname = nil, level = nil, tracer = nil)

  #
  # Change logging level within the YAML config file
  # ! Log4r::YamlConfigurator does not allow to change key/val for the level but only for formatter/outputter
  #
  if level.nil? then new_level = 'INFO' else new_level = level end
  if tracer.nil? then new_tracer = 'false' else new_tracer = tracer end
  if mode.nil? then new_mode = 'production' else new_mode = mode end

  log4r_hash = load_file(log_yml_file)

  #
  # TODO - improve it to avoid to hard code path
  #
  log4r_hash['log4r_config']['loggers'].each_with_index do |x, index|
    log4r_hash['log4r_config']['loggers'][index]['level'] = new_level
    log4r_hash['log4r_config']['loggers'][index]['tracer'] = new_tracer
  end

  cfg = Log4r::YamlConfigurator

  if dirname.nil?
    dir = [Dir.home, 'log'] * '/'
    Dir.mkdir(dir) unless Dir.exist?(dir)
    cfg['DIRNAME'] = dir
  else
    cfg['DIRNAME'] = dirname
  end

  if logname.nil? then cfg['LOGNAME'] = 'hyla.log' else cfg['LOGNAME'] = logname end

  cfg.decode_yaml log4r_hash['log4r_config']

  @log = Log4r::Logger[new_mode]
end

Public Instance Methods

debug(msg) click to toggle source
# File lib/hyla/logger2.rb, line 102
def debug(msg)
  @log.debug msg
end
error(msg) click to toggle source
# File lib/hyla/logger2.rb, line 114
def error(msg)
  @log.error msg
end
fatal(msg) click to toggle source
# File lib/hyla/logger2.rb, line 118
def fatal(msg)
  @log.fatal msg
end
formatted_topic(topic) click to toggle source

topic - the topic of the message, e.g. “Configuration file”, “Deprecation”, etc.

Returns the formatted topic statement

# File lib/hyla/logger2.rb, line 135
def formatted_topic(topic)
  "#{topic} ".rjust(20)
end
info(msg) click to toggle source
# File lib/hyla/logger2.rb, line 106
def info(msg)
  @log.info msg
end
iterate(h, level) click to toggle source
# File lib/hyla/logger2.rb, line 54
def iterate(h, level)
  h.each do |k,v|
    value = v || k
    if value.is_a?(Hash) || value.is_a?(Array)
      puts "evaluating: #{value} recursively..."
      iterate(value, level)
    else
      if k == "level"
        v = level
      end
    end
  end
end
load_file(cfg_file) click to toggle source
# File lib/hyla/logger2.rb, line 78
def load_file(cfg_file)
  SafeYAML::OPTIONS[:default_mode] = :safe

  if cfg_file.nil?
    f = [Configuration.configs,'log4r.yaml' ] * '/'
  else
    f = cfg_file
  end

  #
  # Find/Replace the logging level
  #
  #content = File.read(f)
  #new_contents = content.gsub(/LOGGING_LEVEL/, level)

  # To merely print the contents of the file, use:
  # puts new_contents

  # To write changes to the file, use:
  #File.open(f, "w") {|file| file.puts new_contents }

  YAML.load_file(f)
end
message(topic, message) click to toggle source

topic - the topic of the message, e.g. “Configuration file”, “Deprecation”, etc. message - the message detail

Returns the formatted message

# File lib/hyla/logger2.rb, line 127
def message(topic, message)
  formatted_topic(topic) + message.to_s.gsub(/\s+/, ' ')
end
nested_hash_value(obj,key) click to toggle source
# File lib/hyla/logger2.rb, line 68
def nested_hash_value(obj,key)
  if obj.respond_to?(:key?) && obj.key?(key)
    obj[key]
  elsif obj.respond_to?(:each)
    r = nil
    obj.find{ |*a| r=nested_hash_value(a.last,key) }
    r
  end
end
warn(msg) click to toggle source
# File lib/hyla/logger2.rb, line 110
def warn(msg)
  @log.warn msg
end