Sha256: c0601dd5c1e938937e31312eac351c698794d5b8fa9ffb591a54010bbbc14f17

Contents?: true

Size: 1.75 KB

Versions: 1

Compression:

Stored size: 1.75 KB

Contents

require 'syslog'
require 'logger'

class Syslogger
  attr_reader :level, :ident, :options, :facility

  MAPPING = {
    Logger::DEBUG => Syslog::LOG_DEBUG,
    Logger::INFO => Syslog::LOG_INFO,
    Logger::WARN => Syslog::LOG_NOTICE,
    Logger::ERROR => Syslog::LOG_WARNING,
    Logger::FATAL => Syslog::LOG_ERR,
    Logger::UNKNOWN => Syslog::LOG_ALERT
  }
  # 
  # Initializes default options for the logger
  # <tt>ident</tt>:: the name of your program [default=$0]
  # <tt>options</tt>:: Syslog options [default=Syslog::LOG_PID | Syslog::LOG_CONS]
  # <tt>facility</tt>:: the syslog facility [default=nil]
  #
  #                     correct values are Syslog::LOG_DAEMON, Syslog::LOG_USER, Syslog::LOG_SYSLOG, Syslog::LOG_LOCAL2, Syslog::LOG_NEWS, etc.
  # 
  # Usage:
  #   logger = Syslogger.new("my_app", Syslog::LOG_PID | Syslog::LOG_CONS, Syslog::LOG_LOCAL0)
  #   logger.level = Logger::INFO # use Logger levels
  #   logger.warn "warning message"
  #   logger.debug "debug message"
  # 
  def initialize(ident = $0, options = Syslog::LOG_PID | Syslog::LOG_CONS, facility = nil)
    @ident = ident
    @options = options || (Syslog::LOG_PID | Syslog::LOG_CONS)
    @facility = facility
    @level = Logger::INFO
  end
  
  %w{debug info warn error fatal unknown}.each do |logger_method|
    define_method logger_method.to_sym do |message|
      add(Logger.const_get(logger_method.upcase), message)
    end
  end
  
  def <<(msg)
    add(Logger::INFO, msg)
  end
  
  def add(severity, message = nil, progname = nil, &block)
    progname ||= @ident
    Syslog.open(progname, @options, @facility) { |s| 
      s.mask = Syslog::LOG_UPTO(MAPPING[@level])
      s.log(MAPPING[severity], (message || block.call)) 
    }
  end
  
  def level=(logger_level)
    @level = logger_level
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
syslogger-1.2.0 lib/syslogger.rb