Module: PacketThief::Logging

Overview

Mix-in that provides some private convenience logging functions. Uses the logger specified by this module. To set the current logger for all of PacketThief:

PacketThief::Logging.logger = Logger.new

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.loggerObject

An optional Ruby Logger for debugging output. If it is unset, the log methods will be silent.



13
14
15
# File 'lib/packetthief/logging.rb', line 13

def logger
  @logger
end

Class Method Details

.log(level, component, msg, args = {}) ⇒ Object

Print a message at the specified log severity (prefixed with the component), and display any additional arguments. For example:

log(Logger::DEBUG, SomeClass, "hello", :somevalue => 'that value")

Will print the message: “SomeClass: hello: somevalue=that value” at the debug log level.



22
23
24
25
26
27
28
29
30
# File 'lib/packetthief/logging.rb', line 22

def log(level, component, msg, args={})
  if @logger
    unless args.empty?
      kvstr = args.map { |pair| pair[0].to_s + ': ' + pair[1].inspect }.sort.join(', ')
      msg += ": " + kvstr
    end
    @logger.log(level, component.to_s + ': ' + msg)
  end
end