Class: Log

Inherits:
Object
  • Object
show all
Defined in:
lib/ec2/amitools/util.rb

Overview

Note to self - use log4r next time ;)

Defined Under Namespace

Classes: Facility, Priority, Verbosity

Constant Summary collapse

SYSLOG_OPTS =

—————————————————————————-#

(Syslog::LOG_PID | Syslog::LOG_CONS)
@@facility =

—————————————————————————-#

Facility::AES
@@priority =
Priority::INFO
@@identity =
nil
@@streams_mutex =
Mutex.new
@@streams =
[]

Class Method Summary collapse

Class Method Details

.add_stream(stream) ⇒ Object

Add an additional stream (like a file, or $stdout) to send log output to.



380
381
382
383
384
385
# File 'lib/ec2/amitools/util.rb', line 380

def Log.add_stream(stream)
  @@streams_mutex.synchronize do
    @@streams.push(stream)
    @@streams.delete_if { |io| io.closed? }
  end
end

.debug(msg = nil) ⇒ Object

Log a debug message.



268
269
270
271
272
273
274
# File 'lib/ec2/amitools/util.rb', line 268

def Log.debug(msg=nil)
  if block_given?
    write(Priority::DEBUG) {yield}
  else
    write(Priority::DEBUG) {msg}
  end
end

.err(msg = nil) ⇒ Object

Log an error message.



304
305
306
307
308
309
310
# File 'lib/ec2/amitools/util.rb', line 304

def Log.err(msg=nil)
  if block_given?
    write(Priority::ERR) {yield}
  else
    write(Priority::ERR) {msg}
  end
end

.exception(e) ⇒ Object

Deprecated.

use write

Log an unhandled exception.



329
330
331
332
333
334
335
# File 'lib/ec2/amitools/util.rb', line 329

def Log.exception(e)
  if block_given?
    write(Priority::ALERT) {yield}
  else
    write(Priority::ALERT) {Log.exception_str(e)}
  end
end

.exception_str(e) ⇒ Object

—————————————————————————-#



389
390
391
# File 'lib/ec2/amitools/util.rb', line 389

def Log.exception_str(e)
  e.message + "\n" + e.backtrace.to_s
end

.info(msg = nil) ⇒ Object

Log an informational message.



292
293
294
295
296
297
298
# File 'lib/ec2/amitools/util.rb', line 292

def Log.info(msg=nil)
  if block_given?
    write(Priority::INFO) {yield}
  else
    write(Priority::INFO) {msg}
  end
end

.msg(msg) ⇒ Object

Deprecated.

use write

Log an informational message.



342
343
344
# File 'lib/ec2/amitools/util.rb', line 342

def Log.msg(msg)
  write(Verbosity::V2.to_priority) {msg}
end

.set_facility(facility) ⇒ Object

Set the facility to log messages against when no explicit facility is provided.



430
431
432
# File 'lib/ec2/amitools/util.rb', line 430

def Log.set_facility(facility)
  @@facility = facility
end

.set_identity(identity) ⇒ Object

Set the identity to log messages against when no explicit identity is provided. If no identity is provided (either using this method or explicitly when logging) the system will use the application name as the identity.



441
442
443
# File 'lib/ec2/amitools/util.rb', line 441

def Log.set_identity(identity)
  @@identity = identity
end

.set_io(io) ⇒ Object

Deprecated.

use add_stream

Set the IO instance to log to.



260
261
262
# File 'lib/ec2/amitools/util.rb', line 260

def Log.set_io(io)
  add_stream(io)
end

.set_priority(priority) ⇒ Object

Set the minimum priority of the logging. Messages logged with a lower (less urgent) priority will be ignored.



420
421
422
# File 'lib/ec2/amitools/util.rb', line 420

def Log.set_priority(priority)
  @@priority = priority
end

.set_verbosity(verbosity) ⇒ Object

Deprecated.

use set_priority

Set the verbosity of the logging.



251
252
253
# File 'lib/ec2/amitools/util.rb', line 251

def Log.set_verbosity(verbosity)
  set_priority(verbosity.to_priority)
end

.warn(msg = nil) ⇒ Object

Log a warning message.



280
281
282
283
284
285
286
# File 'lib/ec2/amitools/util.rb', line 280

def Log.warn(msg=nil)
  if block_given?
    write(Priority::WARNING) {yield}
  else
    write(Priority::WARNING) {msg}
  end
end

.write(priority = Priority::DEBUG, facility = nil, identity = nil) ⇒ Object

—————————————————————————-#



459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
# File 'lib/ec2/amitools/util.rb', line 459

def Log.write(priority=Priority::DEBUG, facility=nil, identity=nil)
  # If the priority of this message is below the defined priority
  # for logging then we don't want to do this at all. NOTE: Priorities
  # for syslog are defined in ascending order (so lower priorities
  # are more urgent).
  return unless priority <= @@priority
  return unless block_given?
  
  begin
    facility = (facility == nil)?(@@facility):(facility)
    fac_int = facility.value
    ident = (identity == nil)?(@@identity):(identity)
    msg = yield
    Syslog.open(ident, SYSLOG_OPTS, fac_int) do |log|
      log.log(priority.value, '%s', msg)
    end

    # Now pass the message onto each registered stream
    # Access to our list of streams is synchronized so that it can be changed
    # at runtime.
    @@streams_mutex.synchronize do
      @@streams.each do |stream|
        begin
          stream.puts "#{time}: #{ident}: #{priority.value}: #{msg}"
          stream.flush
        rescue Exception => e
          $stderr.puts 'error writing to stream [#{stream}], logging to stdout'
        end
      end
    end
  rescue Exception => e
    $stderr.puts "error loggin to syslog, logging to stdout: #{e}"
    if block_given?
      begin
        $stdout.puts "Msg: #{msg}"
      rescue Exception => e
        $stderr.puts "Block raised error: #{e}"
      end
 end
  end
end

.xen_msg(msg) ⇒ Object

Deprecated.

use write



349
350
351
# File 'lib/ec2/amitools/util.rb', line 349

def Log.xen_msg(msg)
  write(Verbosity::V4.to_priority) {msg}
end

.xmlrpcfault(xmlrpc_method, fault) ⇒ Object

Deprecated.

use write



370
371
372
# File 'lib/ec2/amitools/util.rb', line 370

def Log.xmlrpcfault(xmlrpc_method, fault)
  write(Verbosity::V3.to_priority) {Log.xmlrpcfault_str(xmlrpc_method, fault)}
end

.xmlrpcfault_str(xmlrpc_method, fault) ⇒ Object

—————————————————————————-#



395
396
397
# File 'lib/ec2/amitools/util.rb', line 395

def Log.xmlrpcfault_str(xmlrpc_method, fault)
  "XML-RPC method fault\nmethod: #{xmlrpc_method}\nfault code: #{fault.faultCode}\nfault string: #{fault.faultString}"
end

.xmlrpcmethod_call(name, *paramstructs) ⇒ Object

Deprecated.

use write



356
357
358
# File 'lib/ec2/amitools/util.rb', line 356

def Log.xmlrpcmethod_call(name, *paramstructs)
  write(Verbosity::V3.to_priority) {Log.xmlrpcmethod_call_str(name, paramstructs)}
end

.xmlrpcmethod_call_str(name, *paramstructs) ⇒ Object

—————————————————————————-#



401
402
403
404
405
# File 'lib/ec2/amitools/util.rb', line 401

def Log.xmlrpcmethod_call_str(name, *paramstructs)
  msg = "name: #{name}\n"
  paramstructs.each_index { |i| msg += "parameter #{i + 1}: #{paramstructs[i].inspect}\n" }
  "XML-RPC method call\n#{msg}"
end

.xmlrpcmethod_return(name, value) ⇒ Object

Deprecated.

use write



363
364
365
# File 'lib/ec2/amitools/util.rb', line 363

def Log.xmlrpcmethod_return(name, value)
  write(Verbosity::V3.to_priority) {Log.xmlrpcmethod_return_str(name, value)}
end

.xmlrpcmethod_return_str(name, value) ⇒ Object

—————————————————————————-#



409
410
411
412
# File 'lib/ec2/amitools/util.rb', line 409

def Log.xmlrpcmethod_return_str(name, value)
  msg = "name: #{name}\nvalue: #{value.inspect}"
  "XML-RPC method return\n#{msg}"
end