Module: Mosquitto::Logging

Included in:
Client
Defined in:
lib/mosquitto/logging.rb

Constant Summary collapse

LOG_LEVELS =
{
  Mosquitto::LOG_ERR => Logger::FATAL,
  Mosquitto::LOG_ERR => Logger::ERROR,
  Mosquitto::LOG_WARNING => Logger::WARN,
  Mosquitto::LOG_INFO => Logger::INFO,
  Mosquitto::LOG_DEBUG => Logger::DEBUG
}

Instance Method Summary collapse

Instance Method Details

#logger=(obj) ⇒ Object

Pipes libmosquitto log messages to a Ruby logger instance.

Examples:

client.logger = Logger.new(STDOUT)

Parameters:

  • logger (String)

    a Ruby logger instance. Compatible with SyslogLogger and other implementations as well.

Raises:

  • (Argument)

    on invalid input params



20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/mosquitto/logging.rb', line 20

def logger=(obj)
  unless obj.respond_to?(:add) and obj.method(:add).arity != 3
    raise ArgumentError, "invalid Logger instance #{obj.inspect}"
  end

  @logger = obj

  on_log do |level, message|
    severity = LOG_LEVELS[level] || Logger::UNKNOWN
    @logger.add(severity, message.to_s, "MQTT")
  end
end