Class: LogSimple::Log

Inherits:
Object
  • Object
show all
Defined in:
lib/logsimple.rb

Overview

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

Class Method Summary collapse

Class Method Details

.add_behaviour(level, *behaviours) ⇒ Object

Adds a number of behaviours for the given level



62
63
64
65
66
67
68
69
70
71
72
73
74
# 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) ⇒ Object

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



84
85
86
87
88
# 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) ⇒ Object

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



121
122
123
124
# File 'lib/logsimple.rb', line 121

def add_observer(&obs)
  @observers ||= Array.new
  @observers.push(obs)
end

.add_parameters(behaviour, params) ⇒ Object

Sets the parameters for a given behaviour.



77
78
79
80
# File 'lib/logsimple.rb', line 77

def add_parameters(behaviour, params)
  @parameters[behaviour] ||= Hash.new
  @parameters[behaviour].merge!(params)
end

.behaviour(level) ⇒ Object

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



51
52
53
# File 'lib/logsimple.rb', line 51

def behaviour(level)
  @behaviour[level]
end

.behavioursObject

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



41
42
43
44
45
46
47
# 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

.do_exit(message, p) ⇒ Object

To terminate execution imediately



236
237
238
# File 'lib/logsimple.rb', line 236

def do_exit(message, p)
  exit
end

.do_log_email(message, p) ⇒ Object

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.



189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# 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) ⇒ Object

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



220
221
222
# 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) ⇒ Object

Logs a message to file. Requires the :file_name parameter



161
162
163
164
165
# 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) ⇒ Object

Logs a message to stderr



151
152
153
# File 'lib/logsimple.rb', line 151

def do_log_stderr(message, p)
  $stderr << message << "\n"
end

.do_log_stdout(message, p) ⇒ Object

Logs a message to stdout



156
157
158
# File 'lib/logsimple.rb', line 156

def do_log_stdout(message, p)
  $stdout << message << "\n"
end

.do_log_syslog(message, p) ⇒ Object

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



171
172
173
174
175
176
177
178
179
180
181
# 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) ⇒ Object

null handler



147
148
# File 'lib/logsimple.rb', line 147

def do_null(message, p)
end

.do_raise(message, p) ⇒ Object

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)



227
228
229
230
231
232
233
# 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

.get_stream(level, pre = '') ⇒ Object

‘def’ method courtesy of www.bigbold.com/snippets/posts/show/2316 Allows us to dynamically add methods to an object with closures.



132
133
134
135
136
137
138
139
140
# File 'lib/logsimple.rb', line 132

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) ⇒ Object

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



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# 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

.mod_append_level(level, message, p) ⇒ Object

Adds the output level in front of the message



241
242
243
# 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) ⇒ Object

Adds the date/time in front of the message



246
247
248
# 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

.remove_parameters(behaviour) ⇒ Object

Removes all parameters associated with a given behaviour



91
92
93
94
95
96
# 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) ⇒ Object

Resets the behaviour for a given log level



56
57
58
59
# File 'lib/logsimple.rb', line 56

def reset_behaviour(level)
  @behaviour[level]  = Array.new
  @parameters[level] = Hash.new
end