Module: FeldtRuby::Logging

Included in:
Optimize::Archive, Optimize::Objective, Optimize::Optimizer
Defined in:
lib/feldtruby/logger.rb

Overview

A simple logging interface front end for classes that need basic logging. Just include and call log methods on logger. Uses a single common logger unless a new one is been explicitly specified..

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#loggerObject

Returns the value of attribute logger.



176
177
178
# File 'lib/feldtruby/logger.rb', line 176

def logger
  @logger
end

Instance Method Details

#__find_logger_set_on_instance_varsObject

Find a logger if one has been set on any of my instance vars or their instance vars (recursively).



223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
# File 'lib/feldtruby/logger.rb', line 223

def __find_logger_set_on_instance_vars

  # First see if we find it in the immediate ivars
  self.instance_variables.each do |ivar_name|

    ivar = self.instance_variable_get ivar_name

    if ivar.respond_to?(:logger)

      begin

        l = ivar.send(:logger)
        return l if l.is_a?(FeldtRuby::Logger)

      rescue Exception => e
      end

    end

  end

  # If we come here it means we did NOT find a logger in immediate
  # ivar's. So we recurse.
  self.instance_variables.each do |ivar_name|

    ivar = self.instance_variable_get ivar_name

    if ivar.respond_to?(:find_logger_set_on_instance_vars)

      begin

        l = ivar.send(:find_logger_set_on_instance_vars)
        return l if l.is_a?(FeldtRuby::Logger)

      rescue Exception => e
      end

    end

  end

  nil

end

#new_default_logger(options = {}) ⇒ Object

Override to use another logger as default if no logger is found.



217
218
219
# File 'lib/feldtruby/logger.rb', line 217

def new_default_logger(options = {})
  FeldtRuby::Logger.new(STDOUT, options)
end

#setup_logger_and_distribute_to_instance_variables(loggerOrOptions = nil, visited = []) ⇒ Object



178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/feldtruby/logger.rb', line 178

def setup_logger_and_distribute_to_instance_variables(loggerOrOptions = nil, visited = [])

  if Hash === loggerOrOptions
    options = loggerOrOptions
    logger = nil
  else
    options = {}
    logger = loggerOrOptions
  end

  # Precedence for loggers if several has been setup:
  #  1. One specified as parameter to this method
  #  2. One that has already been set on this object
  #  3. First one found on an instance var
  #  4. Create a new standard one
  self.logger = logger || self.logger || __find_logger_set_on_instance_vars() ||
    new_default_logger(options)

  # Now distribute the preferred logger to all instance vars, recursively.
  # Save all visited ones to a list though and check that we do not get into
  # infinite regress if two objects refer to each other.
  self.instance_variables.each do |ivar_name|

    ivar = self.instance_variable_get ivar_name

    unless visited.include?(ivar)

      visited << ivar

      if ivar.respond_to?(:setup_logger_and_distribute_to_instance_variables)
        ivar.setup_logger_and_distribute_to_instance_variables self.logger, visited
      end
    end

  end

end