Sha256: b31b7f95532bda26574b7059952c03ccce72342fdc02028ce205b667279f8cfd

Contents?: true

Size: 1.61 KB

Versions: 17

Compression:

Stored size: 1.61 KB

Contents

module Bluepill  
  class Logger
    LOG_METHODS = [:emerg, :alert, :crit, :err, :warning, :notice, :info, :debug]
    
    def initialize(options = {})
      @options = options
      @logger = options[:logger] || self.create_logger
      @prefix = options[:prefix]
      @prefixes = {}
    end
    
    LOG_METHODS.each do |method|
      eval <<-END
        def #{method}(msg, prefix = [])
          if @logger.is_a?(self.class)
            @logger.#{method}(msg, [@prefix] + prefix)
          else
            prefix = prefix.size > 0 ? "[\#{prefix.compact.join(':')}] " : ""
            @logger.#{method}("\#{prefix}\#{msg}")
          end
        end
      END
    end
    
    def prefix_with(prefix)
      @prefixes[prefix] ||= self.class.new(:logger => self, :prefix => prefix)
    end
    
    def reopen
      if @logger.is_a?(self.class)
        @logger.reopen
      else
        @logger = create_logger
      end
    end
    
    protected
    def create_logger
      if @options[:log_file]
        LoggerAdapter.new(@options[:log_file])
      else
        Syslog.close if Syslog.opened? # need to explictly close it before reopening it
        Syslog.open(@options[:identity] || 'bluepilld', Syslog::LOG_PID, Syslog::LOG_LOCAL6)
      end
    end

    class LoggerAdapter < ::Logger
      LOGGER_EQUIVALENTS = 
        {:debug => :debug, :err => :error, :warning => :warn, :info => :info, :emerg => :fatal, :alert => :warn, :crit => :fatal, :notice => :info}
      
      LOG_METHODS.each do |method|
        next if method == LOGGER_EQUIVALENTS[method]
        alias_method method, LOGGER_EQUIVALENTS[method]
      end
    end 
  end
end

Version data entries

17 entries across 17 versions & 3 rubygems

Version Path
bluepill-0.0.36 lib/bluepill/logger.rb
bluepill-0.0.35 lib/bluepill/logger.rb
bluepill-0.0.34 lib/bluepill/logger.rb
bluepill-0.0.33 lib/bluepill/logger.rb
wijet-bluepill-0.0.33 lib/bluepill/logger.rb
bluepill-0.0.32 lib/bluepill/logger.rb
bluepill-0.0.31 lib/bluepill/logger.rb
bluepill-0.0.30 lib/bluepill/logger.rb
bluepill-0.0.28 lib/bluepill/logger.rb
bluepill-0.0.27 lib/bluepill/logger.rb
gvarela-bluepill-0.0.28 lib/bluepill/logger.rb
gvarela-bluepill-0.0.27 lib/bluepill/logger.rb
bluepill-0.0.26 lib/bluepill/logger.rb
bluepill-0.0.25 lib/bluepill/logger.rb
bluepill-0.0.24 lib/bluepill/logger.rb
bluepill-0.0.23 lib/bluepill/logger.rb
bluepill-0.0.22 lib/bluepill/logger.rb