import time
import zmq
from zmq.log.handlers import PUBHandler
import logging
from threading import Thread
LOG_URL = 'tcp://127.0.0.1:12345'
handler = PUBHandler(LOG_URL)
handler.root_topic = 'foo'
logger = logging.getLogger('foobar')
logger.setLevel(logging.DEBUG)
logger.addHandler(handler)
def consume(url):
ctx = zmq.Context.instance()
s = ctx.socket(zmq.SUB)
s.connect(url)
s.subscribe("")
while True:
# msg = s.recv() # return bytes
msg = s.recv_multipart() # return List
# msg = s.recv_multipart(copy=False) # return zmq.Frame
print(type(msg))
print(msg)
s.close()
consumer = Thread(target=consume, args=(LOG_URL,))
consumer.start()
while True:
logger.debug("dddd::aaaa")
time.sleep(1)
<class 'bytes'>
b'foo.DEBUG.dddd'
<class 'bytes'>
b'DEBUG zmq_logging.py:35 - aaaa\n'
<class 'list'>
[b'foo.DEBUG.dddd', b'DEBUG zmq_logging.py:35 - aaaa\n']
<class 'list'>
[<zmq.sugar.frame.Frame object at 0x000002A30E4A1720>, <zmq.sugar.frame.Frame object at 0x000002A30E4A1930>]
|