Sha256: 38ee874be0f5b1532014366d68bbaf46add46a70b3cd10145594a6a6c064005c

Contents?: true

Size: 1.31 KB

Versions: 3

Compression:

Stored size: 1.31 KB

Contents

# looks like this code came from
# http://stackoverflow.com/questions/917566/ruby-share-logger-instance-among-module-classes

require "logger"

module NagiosHerald
  module Logging

    def logger
    @logger ||= Logging.logger_for(self.class.name)
    end

    # Use a hash class-ivar to cache a unique Logger per class:
    # "ivar" = fancy term for "instance variable"
    # do we want this? or a global logger?
    @loggers = {}

    extend self

    # effectively sets the progname
    def logger_for(classname)
      @loggers[classname] ||= configure_logger_for(classname)
    end

    # instantiates a Logger instance
    # default to STDOUT
    def configure_logger_for(classname)
      logfile = Config.config['logfile'] ? Config.config['logfile'] : STDOUT
      if logfile.eql?("STDOUT")
        logfile = STDOUT
      end
      begin
        logger = Logger.new(logfile)
      rescue Exception => e
        puts "Failed to open #{logfile} for writing: #{e.message}"
        puts "Defaulting to STDOUT"
        logger = Logger.new(STDOUT)
      end
      logger.datetime_format = "%Y-%m-%d %H:%M:%S"
      logger.progname = "#{File.basename $0} (#{classname})"
      logger.formatter = proc { |severity, datetime, progname, msg|
        "[#{datetime}] #{severity} -- #{progname}: #{msg}\n"
      }
      logger
    end

  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
nagios-herald-0.0.4 lib/nagios-herald/logging.rb
nagios-herald-0.0.3 lib/nagios-herald/logging.rb
nagios-herald-0.0.2 lib/nagios-herald/logging.rb