zeromq / pyzmq

PyZMQ: Python bindings for zeromq
http://zguide.zeromq.org/py:all
BSD 3-Clause "New" or "Revised" License
3.67k stars 637 forks source link

DOC: Can't use PUBHandler with dictConfig #1839

Closed davetapley closed 1 year ago

davetapley commented 1 year ago

What pyzmq version?

24.0.1

What libzmq version?

4.3.4

Python version (and how it was installed)

3.10.9 via pyenv

OS

ubuntu 20.04

What happened?

I use dictConfig to configure my other loggers, but I needed a workaround (see below) to make it work with PUBHandler.

This happens because the PUBHandler constructor takes socket as an arg, but with dictConfig:

All other keys are passed through as keyword arguments to the handler’s constructor.

Code to reproduce bug

from logging.config import dictConfig

from zmq import PUB, Context

socket = Context.instance().socket(PUB)
socket.connect('tcp://whatever')

config = {
    'version': 1,
    'handlers': {
        'zmq': {
            'class': 'zmq.log.handlers.PUBHandler'
            'socket': socket
        }
    }
}

dictConfig(config)

Traceback, if applicable

TypeError: PUBHandler.__init__() got an unexpected keyword argument 'socket'
The above exception was the direct cause of the following exception:

...

ValueError: Unable to configure handler 'zmq'

More info

Workaround is to create:

class DictConfigPUBHandler(PUBHandler):
    def __init__(self, **kwargs):
        socket = kwargs['socket']
        del kwargs['socket']
        super().__init__(socket, **kwargs)

Then use:

        'zmq': {
            'class': 'path.to.DictConfigPUBHandler'
            'socket': Context.instance().socket(PUB)
minrk commented 1 year ago

The arg name is interface_or_socket, so you should be able to use that. I'll do a PR that allows socket as a kwarg.

I really wonder why I've gotten so many reports about issues in zmq.log in the past few weeks, when it largely hasn't been worked on since 2014. Can I ask where you got the idea to use it?

davetapley commented 1 year ago

Ah I can't believe I didn't see that, yep interface_or_socket works as expected.


I think just complete coincidence for me: working on new project since Jan and ZMQ + Python seemed natural fit. A few weeks ago it dawned on me I could use ZMQ for log transport, and was pleased to find PUBHandler when I Googled "zmq logging" 😁

minrk commented 1 year ago

Fair enough! What do you think about #1840 - which enables socket as kwarg at the expense of some complexity?

davetapley commented 1 year ago

I feel bad you did the work, but yeah I think extra kwarg is overkill when interface_or_socket works correctly.

Perhaps could put dictConfig example on https://pyzmq.readthedocs.io/en/latest/api/zmq.log.handlers.html#module-zmq.log.handlers, for people like me who don't RTFM 😊

I'm happy to make PR by the way, just let me know!

minrk commented 1 year ago

Docs PR with a dictconfig example sounds great! I don't feel bad closing a PR, it's no big deal.