Sha256: 3669aac12554d55b42c0b291994f06c98b4c4fa1f10f50a47bc6012588fe1d28

Contents?: true

Size: 1.78 KB

Versions: 20

Compression:

Stored size: 1.78 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]
      @stdout   = options[:stdout]
      @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
            s_prefix = prefix.size > 0 ? "[\#{prefix.compact.join(':')}] " : ""
            if @stdout
              $stdout.puts("[#{method}]: \#{s_prefix}\#{msg}")
              $stdout.flush
            end
            @logger.#{method}("\#{s_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

20 entries across 20 versions & 6 rubygems

Version Path
bluepill-0.0.46 lib/bluepill/logger.rb
bluepill-0.0.45 lib/bluepill/logger.rb
mwotton-bluepill-0.0.44 lib/bluepill/logger.rb
ra-bluepill-0.0.48 lib/bluepill/logger.rb
ra-bluepill-0.0.47 lib/bluepill/logger.rb
ra-bluepill-0.0.46 lib/bluepill/logger.rb
ra-bluepill-0.0.45 lib/bluepill/logger.rb
ra-bluepill-0.0.44 lib/bluepill/logger.rb
evented_bluepill-0.0.46 lib/bluepill/logger.rb
bluepill-0.0.44 lib/bluepill/logger.rb
sumskyi-bluepill-0.0.42.1 lib/bluepill/logger.rb
sumskyi-bluepill-0.0.42.0 lib/bluepill/logger.rb
bluepill-0.0.43 lib/bluepill/logger.rb
bluepill-0.0.42 lib/bluepill/logger.rb
bluepill-0.0.40 lib/bluepill/logger.rb
dylanvaughn-bluepill-0.0.40 lib/bluepill/logger.rb
dylanvaughn-bluepill-0.0.39 lib/bluepill/logger.rb
bluepill-0.0.39 lib/bluepill/logger.rb
bluepill-0.0.38 lib/bluepill/logger.rb
bluepill-0.0.37 lib/bluepill/logger.rb