module Logging

Public Class Methods

create_logger() click to toggle source

create a new log4r logger with config

# File lib/logging/logging.rb, line 45
def self.create_logger
  log_cfg = Log4r::YamlConfigurator
  conf_file = File.join(File.dirname(__FILE__), "..", "..", "conf","log4r.yml")
  log_cfg.load_yaml_file(conf_file)

  # app logger
  new_logger = Log4r::Logger[@@log_conf]
  raise "Missing logger config #{@@log_conf} in log config file #{conf_file}" unless new_logger
  new_logger.level = Log4r::INFO
  new_logger.info "Log initiated"
  new_logger
end
logger() click to toggle source

Global, memoized, lazy initialized instance of a logger

# File lib/logging/logging.rb, line 59
def self.logger
  @logger ||= create_logger
end
set_log_conf(log_conf) click to toggle source
# File lib/logging/logging.rb, line 39
def self.set_log_conf log_conf
  @@log_conf = log_conf
end

Public Instance Methods

logger() click to toggle source
# File lib/logging/logging.rb, line 24
def logger
  Logging.logger
end
set_log_file(new_filename) click to toggle source
# File lib/logging/logging.rb, line 32
def set_log_file(new_filename)
  format = Log4r::PatternFormatter.new(:pattern => "[%5l] %d :: %m", :date_pattern => '%y%m%d %H:%M:%S')
  dir_name = File.dirname(new_filename)
  file_name = File.basename(new_filename)
  logger.outputters << Log4r::DateFileOutputter.new('log_file', :filename => file_name, :dirname => dir_name, :formatter => format, :date_pattern => '%Y%m%d')
end
set_log_level(level_str) click to toggle source
# File lib/logging/logging.rb, line 28
def set_log_level(level_str)
  logger.level = (Log4r::Log4rConfig::LogLevels.index level_str.upcase) + 1
end