Module: Logging

Included in:
ChefFiles, DeviceInfo, DeviceInfo, GGConfig, GaddyGaddy_Client, Interval, Notification::Send, Request
Defined in:
lib/logging/logging.rb

Constant Summary collapse

@@log_conf =

This is the magical bit that gets mixed into your classes

const_defined?(:APPLICATION_NAME) ? APPLICATION_NAME : "default"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.create_loggerObject

create a new log4r logger with config



45
46
47
48
49
50
51
52
53
54
55
56
# 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

.loggerObject

Global, memoized, lazy initialized instance of a logger



59
60
61
# File 'lib/logging/logging.rb', line 59

def self.logger
  @logger ||= create_logger
end

.set_log_conf(log_conf) ⇒ Object



39
40
41
# File 'lib/logging/logging.rb', line 39

def self.set_log_conf log_conf
  @@log_conf = log_conf
end

Instance Method Details

#loggerObject



24
25
26
# File 'lib/logging/logging.rb', line 24

def logger
  Logging.logger
end

#set_log_file(new_filename) ⇒ Object



32
33
34
35
36
37
# 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) ⇒ Object



28
29
30
# File 'lib/logging/logging.rb', line 28

def set_log_level(level_str)
  logger.level = (Log4r::Log4rConfig::LogLevels.index level_str.upcase) + 1
end