module PacketThief::Logging

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

Attributes

logger[RW]

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

Public Class Methods

log(level, component, msg, args={}) click to toggle source

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.

# 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

Private Instance Methods

logdebug(msg, args={}) click to toggle source
# File lib/packetthief/logging.rb, line 36
def logdebug(msg, args={})
  Logging.log(Logger::DEBUG, (([Module, Class].include? self.class) ? self.name : self.class), msg, args)
end
logerror(msg, args={}) click to toggle source
# File lib/packetthief/logging.rb, line 48
def logerror(msg, args={})
  Logging.log(Logger::ERROR, self.class, msg, args)
end
logfatal(msg, args={}) click to toggle source
# File lib/packetthief/logging.rb, line 52
def logfatal(msg, args={})
  Logging.log(Logger::FATAL, self.class, msg, args)
end
loginfo(msg, args={}) click to toggle source
# File lib/packetthief/logging.rb, line 40
def loginfo(msg, args={})
  Logging.log(Logger::INFO, self.class, msg, args)
end
logwarn(msg, args={}) click to toggle source
# File lib/packetthief/logging.rb, line 44
def logwarn(msg, args={})
  Logging.log(Logger::WARN, self.class, msg, args)
end