module Logging

Creates for each implementing object a member @log and precede its output with the name of the class of the object.

Public Instance Methods

init_logger(target = STDOUT, level = Logger::INFO) click to toggle source
# File lib/logging.rb, line 31
def init_logger(target = STDOUT, level = Logger::INFO)
        # allow to override the set log-levels with an
        # external configuration (log.conf).
        log_conf
        # 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
                "#{self.class.name}: #{severity} #{t.hour}-#{t.min}-#{t.sec}: #{msg}\n"
        end
        if ! @@have_log
                @log.debug self.class.name.dup << ' reading logging-configuration from ' << @@LOG_CONF       
                @@have_log = true
        end

end
log_level=(level) click to toggle source
# File lib/logging.rb, line 59
def log_level=(level)
        @level = level
        @log.level = @level
end
log_target=(target) click to toggle source
# File lib/logging.rb, line 54
def log_target=(target)
        @target = target
        @log = Logger.new(@@target)
        @log.level = @level
end

Private Instance Methods

byte_units(bytes) click to toggle source
# File lib/logging.rb, line 66
def byte_units(bytes)
        m = bytes / 1000000
        k = (bytes - m) / 1000
        b = bytes - m - k
        
end
log_conf() 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 77
def log_conf
        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
                config = config[self.class.name.to_sym]
                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) ? ' not ' << m.to_s : nil)
                                        if(msg) 
                                                STDERR.puts "WARNING! A logfile for '%s' cannot be written to %s (%s)!" %[self.class.name, logdir, msg] 
                                                @target = nil
                                        end

                                end
                                # msg = file_check(logdir, :exist?, :directory?, :writable?)
                        else
                                @level = config
                        end 
                end
        end
end