The Log class - a static class through which all the logging is done. See the README for usage instructions.

Contents
Methods
Public Class methods
add_behaviour(level, *behaviours)

Adds a number of behaviours for the given level

# File lib/logsimple.rb, line 62
    def add_behaviour(level, *behaviours)
      @behaviour[level] ||= Array.new
      @behaviour[level].concat(behaviours)
      
      name = "#{level.to_s}"
      if !method_defined?(name)
        class_eval("class <<self
                      def #{name}(message, inparams = Hash.new)
                        log(message, \"#{level.to_s}\".to_sym, inparams)
                      end
                    end")
      end
    end
add_level_parameters(level, behaviour, params)

Sets the parameters for a given behaviour for a given level. These overide the ones given for a given behaviour only.

# File lib/logsimple.rb, line 84
    def add_level_parameters(level, behaviour, params)
      @level_parameters[level] ||= Hash.new
      @level_parameters[level][behaviour] ||= Hash.new
      @level_parameters[level][behaviour].merge!(params)
    end
add_observer(&obs)

Adds an observer to the class. Those are called with the message before the message gets logged

# File lib/logsimple.rb, line 121
    def add_observer(&obs)
      @observers ||= Array.new
      @observers.push(obs)
    end
add_parameters(behaviour, params)

Sets the parameters for a given behaviour.

# File lib/logsimple.rb, line 77
    def add_parameters(behaviour, params)
      @parameters[behaviour] ||= Hash.new
      @parameters[behaviour].merge!(params)
    end
behaviour(level)

Query the behaviour for a given log level. Returns an array with the behaviour flags that apply.

# File lib/logsimple.rb, line 51
    def behaviour(level)
      @behaviour[level]
    end
behaviours()

Query all the possible behaviours, as defined by class instance methods do_#{behaviour}

# File lib/logsimple.rb, line 41
    def behaviours
      res = Array.new
      methods.each do |method|
        if method =~ /^(do|mod)_(.+)$/ then res.push($2.to_sym) end
      end
      res
    end
get_stream(level, pre = '')

Gets a stream object for the given log level

# File lib/logsimple.rb, line 127
    def get_stream(level, pre = '')
      o = Object.new
      
      # 'def' method courtesy of http://www.bigbold.com/snippets/posts/show/2316
      # Allows us to dynamically add methods to an object with closures.
      def o.def(name, &block)
        (class << self; self end).send(:define_method, name, block)
      end
      o.def(:<<) do |s|
        Log.log(pre.to_s + s.to_s, level)
        self
      end
      o
    end
log(message, level = :error, inparams = Hash.new)

Log a message with the given level, and parameters (as a hash). These parameters override the ones given per level or per behaviour.

# File lib/logsimple.rb, line 100
    def log(message, level = :error, inparams = Hash.new)
      @observers ||= Array.new
      @observers.each {|p| p.call(message)}
      
      @behaviour[level].each do |b|
        params = Hash.new
        params.merge!(@parameters[b]) if @parameters.has_key?(b)
        params.merge!(@level_parameters[level][b]) if @level_parameters.has_key?(level) &&
                                                       @level_parameters[level].has_key?(b)
        params.merge!(inparams)
        
        if respond_to?("mod_#{b}".to_sym, true)
          message = send("mod_#{b}".to_sym, level, message, params)
        elsif respond_to?("do_#{b}".to_sym, true)
          send("do_#{b}".to_sym, message, params)
        end
      end
    end
remove_parameters(behaviour)

Removes all parameters associated with a given behaviour

# File lib/logsimple.rb, line 91
    def remove_parameters(behaviour)
      @parameters.delete(behaviour)
      @level_parameters.each_value do |h|
        h.delete(behaviour)
      end
    end
reset_behaviour(level)

Resets the behaviour for a given log level

# File lib/logsimple.rb, line 56
    def reset_behaviour(level)
      @behaviour[level]  = Array.new
      @parameters[level] = Hash.new
    end

The following are the individual predefined behaviour handlers

Public Class methods
do_exit(message, p)

To terminate execution imediately

# File lib/logsimple.rb, line 236
    def do_exit(message, p)
      exit
    end
do_log_email(message, p)

Sends an email with the log (SMTP only). Requires :smtp_server, :smtp_username, :smtp_password, :from and :to (may be an array) parameters to be set. If the optional parameter :subject is set, then it is used for subject instead of $0 ; if the optional parameter :smtp_port is set then it is used for port instead of 25 ; if the optional parameter :smtp_helo is set then it is used for HELO message ; if the optional parameter smpt_authtype is set, then it is used to determine the type of authentification (:plain, :login or :cram_md5). :plain is used by default.

# File lib/logsimple.rb, line 189
    def do_log_email(message, p)
      port = 25
      port = p[:smtp_port] if p.has_key?(:smtp_port)
      helo = nil
      helo = p[:smtp_helo] if p.has_key?(:smtp_helo)
      authtype = :plain
      authtype = p[:smtp_authtype] if p.has_key?(:smtp_authtype)
      subject = $0
      subject = p[:subject] if p.has_key?(:subject)
      p[:from] = p[:from].joing(',') if (p[:from].kind_of? Array)
      date_str = Time.now.strftime("%c")
      mid = Digest::MD5.hexdigest(date_str + "-" + message).to_s
      
      msgstr = "
      From: #{p[:from]}
      To: #{p[:to]}
      Subject: #{subject}
      Date: #{date_str}
      Message-Id: <#{mid}>
      
      #{message}

      "

      Net::SMTP.start(p[:smtp_server], port, helo, p[:smtp_username], p[:smtp_password], 
                      authtype) do |smtp|
        smtp.send_message(msgstr, p[:from], p[:to])
      end
    end
do_log_exec(message, p)

Runs an executable with the log. Requires :executable parameter to be set

# File lib/logsimple.rb, line 220
    def do_log_exec(message, p)
      exec(p[:executable].to_s + " " +message) if fork.nil?
    end
do_log_file(message, p)

Logs a message to file. Requires the :file_name parameter

# File lib/logsimple.rb, line 161
    def do_log_file(message, p)
      File.open(p[:file_name], 'a+') do |f|
        f << message << "\n"
      end
    end
do_log_stderr(message, p)

Logs a message to stderr

# File lib/logsimple.rb, line 151
    def do_log_stderr(message, p)
      $stderr << message << "\n"
    end
do_log_stdout(message, p)

Logs a message to stdout

# File lib/logsimple.rb, line 156
    def do_log_stdout(message, p)
      $stdout << message << "\n"
    end
do_log_syslog(message, p)

Logs to syslog (when available). Will use the optional parameters :name and :level. :name represents the application name (will use $0 if not set) and :level represents the syslog error level. If not given, will log at syslog level ‘error’. The options are debug, info, notice, warning, err, alert, emerg and crit

# File lib/logsimple.rb, line 171
    def do_log_syslog(message, p)
      level = 'err'
      app = $0
      
      level = p[:level] if p.has_key?(:level)
      app = p[:name] if p.has_key?(:name)
      
      log = Syslog.open(app)
      log.send(level.to_sym, message)
      log.close
    end
do_null(message, p)

null handler

# File lib/logsimple.rb, line 147
    def  do_nulldo_null(message, p)
    end
do_raise(message, p)

Raises an exception with the log. Will use the optional :class_name parameter to raise the execption (otherwise, raises a RuntimeError exception). #{:class_name}.new should take one string parameter (the log message)

# File lib/logsimple.rb, line 227
    def do_raise(message, p)
      if (p.has_key?(:class_name))
        raise Kernel.const_get(p[:class_name]).new(message)
      end
      
      raise message
    end
mod_append_level(level, message, p)

Adds the output level in front of the message

# File lib/logsimple.rb, line 241
    def mod_append_level(level, message, p)
      "[" + level.to_s + "] " + message.to_s
    end
mod_append_time(level, message, p)

Adds the date/time in front of the message

# File lib/logsimple.rb, line 246
    def mod_append_time(level, message, p)
      "[" + Time.now.strftime("%H:%M:%S %d/%m/%y") + "] " + message.to_s
    end