The Log class - a static class through which all the logging is done. See the README for usage instructions.
- add_behaviour
- add_level_parameters
- add_observer
- add_parameters
- behaviour
- behaviours
- do_exit
- do_log_email
- do_log_exec
- do_log_file
- do_log_stderr
- do_log_stdout
- do_log_syslog
- do_null
- do_raise
- get_stream
- log
- mod_append_level
- mod_append_time
- remove_parameters
- reset_behaviour
Adds a number of behaviours for the given level
[ show source ]
# 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
Sets the parameters for a given behaviour for a given level. These overide the ones given for a given behaviour only.
[ show source ]
# 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
Adds an observer to the class. Those are called with the message before the message gets logged
[ show source ]
# File lib/logsimple.rb, line 121 def add_observer(&obs) @observers ||= Array.new @observers.push(obs) end
Sets the parameters for a given behaviour.
[ show source ]
# File lib/logsimple.rb, line 77 def add_parameters(behaviour, params) @parameters[behaviour] ||= Hash.new @parameters[behaviour].merge!(params) end
Query the behaviour for a given log level. Returns an array with the behaviour flags that apply.
[ show source ]
# File lib/logsimple.rb, line 51 def behaviour(level) @behaviour[level] end
Query all the possible behaviours, as defined by class instance methods do_#{behaviour}
[ show source ]
# 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
Gets a stream object for the given log level
[ show source ]
# 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 a message with the given level, and parameters (as a hash). These parameters override the ones given per level or per behaviour.
[ show source ]
# 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
Removes all parameters associated with a given behaviour
[ show source ]
# File lib/logsimple.rb, line 91 def remove_parameters(behaviour) @parameters.delete(behaviour) @level_parameters.each_value do |h| h.delete(behaviour) end end
Resets the behaviour for a given log level
[ show source ]
# 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
To terminate execution imediately
[ show source ]
# File lib/logsimple.rb, line 236 def do_exit(message, p) exit end
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.
[ show source ]
# 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
Runs an executable with the log. Requires :executable parameter to be set
[ show source ]
# File lib/logsimple.rb, line 220 def do_log_exec(message, p) exec(p[:executable].to_s + " " +message) if fork.nil? end
Logs a message to file. Requires the :file_name parameter
[ show source ]
# 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
Logs a message to stderr
[ show source ]
# File lib/logsimple.rb, line 151 def do_log_stderr(message, p) $stderr << message << "\n" end
Logs a message to stdout
[ show source ]
# File lib/logsimple.rb, line 156 def do_log_stdout(message, p) $stdout << message << "\n" end
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
[ show source ]
# 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
null handler
[ show source ]
# File lib/logsimple.rb, line 147 def do_nulldo_null(message, p) end
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)
[ show source ]
# 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
Adds the output level in front of the message
[ show source ]
# File lib/logsimple.rb, line 241 def mod_append_level(level, message, p) "[" + level.to_s + "] " + message.to_s end
Adds the date/time in front of the message
[ show source ]
# 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