Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PUBHandler prepends loglevel, module and line to log message #1253

Open
Blindfreddy opened this issue Jan 14, 2019 · 7 comments
Open

PUBHandler prepends loglevel, module and line to log message #1253

Blindfreddy opened this issue Jan 14, 2019 · 7 comments

Comments

@Blindfreddy
Copy link

I have successfully implemented a PUBHandler, however, for all loglevels except info the subscriber seems to receive a message bytestring which has prepended the loglevel as a string, the originating module name and the line in the module.

Examples:
INFO - seems fine
2019-01-14 23:38:49,764 INFO TS_T1: starting cycle...

DEBUG - loglevel, module name and line prepended to the log message
2019-01-14 23:38:49,765 DEBUG **DEBUG sensor.py:191** - TS_T1: take_reading...

ERROR- same as debug plus appends "- None"
2019-01-14 23:42:48,936 ERROR **ERROR foundation.py:489** - TS_T1: starting cycle...done. **- None**
CRITICAL - same as debug
2019-01-14 23:38:49,770 CRITICAL **CRITICAL foundation.py:489** - TS_T1: starting cycle...done.

It can be filtered with message.split(b" - ")[-1:][0].decode('utf-8') but that seems awkward. Is there a way to suppress this at the source, or am I perhaps not understanding the way PUBHandler emits logrecords fully ?

Many thanks.

@Blindfreddy
Copy link
Author

Further to my above post, I either don't understand how to use PUBHandler, or it is not really useful in its current form. I'd suggest to change it so that it receives a logrecord object via recv_pyobj() which can then be handled by passing it to the existing logger(s):

record = recv_pyobj()
logger.handle(record)

@guidog
Copy link
Contributor

guidog commented Feb 5, 2019

Wrong project?

@guidog
Copy link
Contributor

guidog commented Feb 7, 2019

Issue can be removed, I think.

@minrk
Copy link
Member

minrk commented Feb 25, 2019

PUBHandler was written a very long time ago, and should probably be updated quite a bit. I don't think I properly understood what this should do. If you'd like to take a stab at updating it, that would be cool. I wouldn't use pyobj methods (those should generally not be used in production), but I would structure the messages differently to preserve the logrecord format.

@Blindfreddy
Copy link
Author

Blindfreddy commented Feb 25, 2019

Ok, that explains why it doesn't really work nowadays. Just to be clear, all I wanted to use for was to handle logging to a single stream in a multiprocess python program. Since I already use zmq for inter-process communication, it wasn't a big leap to add PUBHandler. In the end I managed to achieve the same thing by way of a Queue object as described in the python mp documentation.

Apart from all the surrounding 'stuff', there are really only two lines of code in the mp logger class:

    def logger_thread_run(self, logger_q):
        msg = ("{}: logger_thread started, threads: {}."
               .format(self.name, enumerate()))
        self.logger.debug(msg)
        try:
            while True:
                try:
                    **record = logger_q.get()**                    
                    **self.logger.handle(record)**
                except Exception:
                    import sys, traceback
                    print('{}: unexpected exception.'.format(self.name), file=sys.stderr)
                    traceback.print_exc(file=sys.stderr)
        finally:
            print(msg + " has ended.") 

logger_q is the shared Queue, when a record arrives it just passes it to the local logger.
In my opinion, the PUBHandler should do exactly the same - listen for a logrecord to arrive and then pass it to the local logger(s).

Assuming that's what is desired (?) then I could have a stab at updating it accordingly.

@minrk
Copy link
Member

minrk commented Feb 27, 2019

Yeah, that sounds great. The one thing I would do is make sure to serialize the fields as e.g. JSON not the record object as pickle, since pickle raises unnecessary security issues on the receiving end.

@klovanone
Copy link

klovanone commented Jul 9, 2019

Hi, I recently needed similar functionality (passing logrecords over zmq and serializing/deserializing) and ended up writing my own version for a project that I am working on.

It is here if it is of any use to anyone:
https://github.com/klover1/pyzmq/tree/logrecordpubhandler

I have been using it for quite a while with no problems but please let me know if there are any issues with it.

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants