module Logging
of the object. Example for a class-level logger: # ——————– class TClass
self.extend(Logging) @@log = init_logger(STDOUT) def test_log @@log.info('class-level logger called from instance: ' << @@log.to_s) @log = @@log @log.info('AGAIN: class-level logger called from instance: ' << @log.to_s) end def self::test_log @log.info('class-level logger called from class: ' << @log.to_s) @@log.info('AGAIN: class-level logger called from class: ' << @@log.to_s) end
end
Public Instance Methods
init_logger(target = STDOUT, level = Logger::INFO)
click to toggle source
Call this method in an instance-method (e.g. initialize() ) to define the object-level logger; i.e. an object-specific member @log. Call this method within the class-definition for a class-level logger; i.e. a member @log for class-level acces. The method returns the logger, so you can actually do what you want with it.
# File lib/logging.rb, line 65 def init_logger(target = STDOUT, level = Logger::INFO) # Prepare for a class-level logger. This is actually quite cool. # ---> Ingeniuous code starts here cn = (self.class == Class ? name : self.class.name) # <--- Ingeniuous code ends here # allow to override the set log-levels with an # external configuration (log.conf). log_conf(cn) # Or use the defaults as set here or elsewhere... @level ||= level @target ||= target @log = Logger.new(@target) @log.level = @level @log.formatter = proc do |severity, datetime, progname, msg| t = Time.now "#{cn}: #{severity} #{t.hour}-#{t.min}-#{t.sec}: #{msg}\n" end if ! @@have_log @log.debug cn.dup << ' reading logging-configuration from ' << @@LOG_CONF @@have_log = true @log.debug('level is ' << level.to_s) end return @log end
log_level=(level)
click to toggle source
set the log-level
# File lib/logging.rb, line 103 def log_level=(level) @level = level @log.level = @level end
log_target=(target)
click to toggle source
Set the log-target to an IO object.
# File lib/logging.rb, line 96 def log_target=(target) @target = target @log = Logger.new(@@target) @log.level = @level end
Private Instance Methods
log_conf(cn = nil)
click to toggle source
Override or set the log-level and target-device, as set in a file 'log.conf'. I do not like the look of this, but it works just the way I want it to. “HEAVANS! Isn't there a standard way to do this in Ruby, for Christ's sake?”, you say. Heck, I don't care. <= Read that again, I say.
# File lib/logging.rb, line 114 def log_conf(cn = nil) config = level = target = nil # puts 'log-config is in ' << @@LOG_CONF if(File::exist?(@@LOG_CONF) ) begin conf = File.read(@@LOG_CONF) config = instance_eval(conf) rescue Exception => ex STDERR.puts "WARNING! Cannot evaluate the logger-configuration!" << ' ' << ex.message STDERR.puts "Default log-levels apply." end else puts "Default log-levels apply." end if(config && config.respond_to?(:to_hash) ) config.default = nil if cn config = config[cn.to_sym] else config = config[self.class.name.to_sym] end if(config ) if(config.respond_to?(:to_ary) && config.size == 2) @level, @target = config @target.downcase! logdir = File.dirname(@target) [:exist?, :directory?, :writable?].each do |m| msg = File.send(m, logdir) if(msg) STDERR.puts "WARNING! A logfile for '%s' cannot be written to %s (%s)!" %[self.class.name, logdir, msg] @target = nil end end else @level = config end end end end