Sha256: bbe385c1983066682c9f9386cc1e04be2fcd66405c0d3b471f126dc66bd5dbe4

Contents?: true

Size: 1.55 KB

Versions: 10

Compression:

Stored size: 1.55 KB

Contents

# encoding: utf-8

require 'logger'

module ZMQ

  # A logging class that sends its output to a ZMQ socket. This allows log messages to be sent
  # asynchronously to another process and across a network if required. The receiving socket
  # will receive messages, each with text of one log message.
  #
  # Example usage:
  #
  #   socket = context.socket(ZMQ::PUSH)
  #   socket.connect("tcp://logserver:5555")
  #   logger = ZMQ::Logger.new(socket)
  #   logger.debug("Hello logger")
  #
  class Logger < ::Logger

    class InvalidSocketError < ZMQ::Error ; end

    # only these socket classes are allowed to be used for sending
    VALID_SOCKET_CLASSES = [
      Socket::Pub,
      Socket::Push,
      Socket::Pair
    ]

    # Initialise a new logger object. The logger sends log messages to a socket.
    # The caller is responsible for connecting the socket before using the logger to
    # send log output to the socket.
    #
    # Supported socket types are a Socket::Pub, Socket::Push, and Socket::Pair
    def initialize(socket)
      raise InvalidSocketError unless VALID_SOCKET_CLASSES.any? { |klass| socket.is_a?(klass) }
      super(nil)
      @logdev = LogDevice.new(socket)
    end

    # implements an interface similar to ::Logger::LogDevice. This is the recipient of the
    # formatted log messages
    class LogDevice
      # :nodoc:
      def initialize(socket)
        @socket = socket
      end

      # write the formatted log message to the socket.
      def write(message)
        @socket.send(message)
      end

      def close
      end
    end

  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
rbczmq-1.7.9 lib/zmq/logger.rb
rbczmq-1.7.8 lib/zmq/logger.rb
rbczmq-1.7.7 lib/zmq/logger.rb
rbczmq-1.7.6 lib/zmq/logger.rb
rbczmq-1.7.5 lib/zmq/logger.rb
rbczmq-1.7.4 lib/zmq/logger.rb
rbczmq-1.7.3 lib/zmq/logger.rb
rbczmq-1.7.2 lib/zmq/logger.rb
rbczmq-1.7.1 lib/zmq/logger.rb
rbczmq-1.7.0 lib/zmq/logger.rb