Sha256: 2acde228e430e2a883d5960657e00fcf29c6b282b7bd4caa850ffb8dc24f56bd

Contents?: true

Size: 1.3 KB

Versions: 1

Compression:

Stored size: 1.3 KB

Contents

# encoding: utf-8

module Mosquitto::Logging
  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
  }

  attr_reader :logger

  # Pipes libmosquitto log messages to a Ruby logger instance.
  #
  # @param logger [Logger] a Ruby logger instance. Compatible with SyslogLogger and other
  #                        implementations as well.
  # @raise [Argument] on invalid input params
  # @example
  #   client.logger = Logger.new(STDOUT)
  #
  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

  # Pipe debug messages through an already assigned logger instance.
  #
  # @param message [string] a message to log
  # @param severity [Mosquitto::LOG_ERR, Mosquitto::LOG_WARNING, Mosquitto::LOG_INFO, Mosquitto::LOG_DEBUG] log severity
  # @example
  #   client.log("message")
  #
  def log(message, severity = Logger::DEBUG)
    logger.add(severity, message.to_s, "MQTT") if logger
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
mosquitto-0.3 lib/mosquitto/logging.rb