Module: OMF::Common::Loggable

Constant Summary collapse

@@logger =
nil
@@rootLoggerName =
nil

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.init_log(appName, opts = {}) ⇒ Object

Initialize the logger. The ‘appName’ is used to build some of the defaults. The ‘environment’ is the name of the root logger. ‘AppInstance’ and ‘appName’ are available as parameters in the log configuration file. The ‘opts’ hash can optionally contain information on how to find a configuration file. The following keys are used:

* :environment - Name used for root logger name ['development']
* :env - Name of environment variable holding dominant config file
* :fileName - Name of config file [#{appName}_log.xml]
* :searchPath - Array of directories to look for 'fileName'


60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/omf_common/lobject.rb', line 60

def self.init_log(appName, opts = {})
 
  #@@logger = ::Log4r::Logger.new(appName)
  set_environment(opts[:environment] || 'development')
  @@logger = ::Log4r::Logger.new(@@rootLoggerName)
  
  configFile = opts[:configFile]
  if (configFile == nil && logEnv = opts[:env])
      configFile = ENV[logEnv]
  end
  if (configFile != nil)
    # Make sure log exists ...
    configFile = File.exists?(configFile) ? configFile : nil
  else
    name = opts[:fileName] || "#{appName}_log4r.yaml"
    if ((searchPath = opts[:searchPath]) != nil)
      searchPath = searchPath.is_a?(Enumerable) ? searchPath : [searchPath]
      logDir = searchPath.find {|dir|
        File.exists?("#{dir}/#{name}")
      }
      #puts "logDir '#{logDir}:#{logDir.class}'"
      configFile = "#{logDir}/#{name}" if logDir != nil
    end
  end
  #puts "config file '#{configFile}'"
  if !(configFile || '').empty?
    ::Log4r::Configurator['appName'] = appName
    begin
      ycfg = YAML.load_file(configFile)
      ::Log4r::YamlConfigurator.decode_yaml(ycfg['log4r'])
      #::Log4r::Configurator.load_xml_file(configFile)
    # rescue ::Log4r::ConfigError => ex
      # @@logger.outputters = ::Log4r::Outputter.stdout
      # # TODO: FIX ME
      # puts("ERROR: Log::Config: #{ex}")
    end
  else
    # set default behavior
    ::Log4r::Logger.global.level = ::Log4r::ALL
    formatter = ::Log4r::PatternFormatter.new(:pattern => "%l %c: %m")
    ::Log4r::StdoutOutputter.new('console', :formatter => formatter)
    @@logger.add 'console'
    #@@logger.outputters = ::Log4r::StdoutOutputter.new('console') #Outputter.stdout
    ##@@logger.outputters = ::Log4r::Outputter.stdout
  end
end

.logger(category) ⇒ Object



116
117
118
119
120
121
122
123
124
125
# File 'lib/omf_common/lobject.rb', line 116

def self.logger(category)
  raise "Logger not initialized" unless @@logger

  name = "#{@@rootLoggerName}::#{category}"
  logger = Log4r::Logger[name]
  if logger == nil
    logger = Log4r::Logger.new(name)
  end
  return logger
end

.set_environment(root_logger_name) ⇒ Object



107
108
109
110
111
112
113
114
# File 'lib/omf_common/lobject.rb', line 107

def self.set_environment(root_logger_name)
  if root_logger_name.nil? || root_logger_name.empty?
    # TODO: FIX ME
    puts("ERROR: LObject: Ignoring empty root logger")
    return
  end
  @@rootLoggerName = root_logger_name
end

Instance Method Details

#_logger(category = nil) ⇒ Object



152
153
154
155
156
157
158
159
160
161
# File 'lib/omf_common/lobject.rb', line 152

def _logger(category = nil)
  unless @logger #&& category.nil?
    cat = self.class.to_s 
    if category
      cat = "#{cat}-#{category}"
    end
    @logger = OMF::Common::Loggable.logger(cat)
  end
  return @logger
end

#debug(*message) ⇒ Object



127
128
129
130
# File 'lib/omf_common/lobject.rb', line 127

def debug(*message)
  logger = _logger()
  logger.debug(message.join('')) if logger.debug?
end

#error(*message) ⇒ Object



142
143
144
145
# File 'lib/omf_common/lobject.rb', line 142

def error(*message)
  logger = _logger()
  logger.error(message.join('')) if logger.error?
end

#fatal(*message) ⇒ Object



147
148
149
150
# File 'lib/omf_common/lobject.rb', line 147

def fatal(*message)
  logger = _logger()
  logger.fatal(message.join('')) if logger.fatal?
end

#info(*message) ⇒ Object



132
133
134
135
# File 'lib/omf_common/lobject.rb', line 132

def info(*message)
  logger = _logger()
  logger.info(message.join('')) if logger.info?
end

#warn(*message) ⇒ Object



137
138
139
140
# File 'lib/omf_common/lobject.rb', line 137

def warn(*message)
  logger = _logger()
  logger.warn(message.join('')) if logger.warn?
end