module Logging

Creates for each implementing object a member @log and precede its output with the name of the class of the object. See also the log-configuration file.

Public Instance Methods

init_logger(target = STDOUT, level = Logger::INFO) click to toggle source
# File lib/logging.rb, line 37
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...

        @def_log_level ||= level 
        @target ||= target

        @log = Logger.new(@target)
        @log.level = @def_log_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 64
def log_level=(level)
        @def_log_level = level
        @log.level = @def_log_level
end
log_target=(target) click to toggle source
# File lib/logging.rb, line 59
def log_target=(target)
        @target = target
        @log = Logger.new(@@target)
        @log.level = @def_log_level
end

Private Instance Methods

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 74
def log_conf
        config = level = target = nil
        if(File.exist?(@@LOG_CONF) )
                begin
                        conf = File.read(@@LOG_CONF)
                        config = instance_eval(conf)
                rescue Exception => ex
                        STDERR.puts trl("WARNING! Cannot evaluate the logger-configuration!") << ' ' << ex.message
                        STDERR.puts trl("Default log-levels apply.")
                end
        else
                        puts trl("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)
                                @def_log_level, @target = config
                                logdir = File.dirname(@target)
                                msg = file_check(logdir, :exist?, :directory?, :writable?)
                                if(msg) 
                                        STDERR.puts trl("WARNING! A logfile for '%s' cannot be written to %s (%s)!") %[self.class.name, logdir, msg] 
                                        @target = nil
                                end
                        else
                                @def_log_level = config
                        end 
                end
        end
end