[Python-bugs-list] [ python-Bugs-760703 ] SocketHandler and LogRecord don't work well together

SourceForge.net noreply@sourceforge.net
Thu, 26 Jun 2003 10:45:26 -0700


Bugs item #760703, was opened at 2003-06-25 12:56
Message generated for change (Comment added) made by jaraco
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=760703&group_id=5470

Category: Python Library
Group: Python 2.3
Status: Open
Resolution: None
Priority: 5
Submitted By: Jason R. Coombs (jaraco)
Assigned to: Nobody/Anonymous (nobody)
Summary: SocketHandler and LogRecord don't work well together

Initial Comment:
>From the logging module, I'm using the DatagramHandler 
(or SocketHandler) to send log messages via UDP 665.  
When I receive them on the other side, there's no 
elegant way to reconstruct them on the other side.  For 
example,

--- in one python session ---
>>> import logging, logging.handlers
>>> handler = logging.handlers.DatagramHandler
( 'localhost', 695 )
>>> logging.root.addHandler( handler )
>>> logging.root.info( 'test' )

--- and in another ---
>>> from socket import *
>>> import pickle, logging
>>> s = socket( AF_INET, SOCK_DGRAM )
>>> s.bind( ( '', 695 ) )
>>> rec = s.recv( 32768 )[4:] #ignore length for now
>>> rec = pickle.loads( rec )
>>> type( rec )
<type 'dict'>
>>> newRec = logging.LogRecord( '', '', '', 1, '', (), None )
>>> newRec.__dict__ = rec

Because logging.LogRecord is not a dict or derived from 
dict, I have to create a log record using 7 arguments, 
then assign the unpickled dict... which is not pretty to 
say the least.

Furthermore, the documentation dictates that the 
record is pickled, not the record's __dict__.

I changed line 163 in logging.handlers to:
        s = cPickle.dumps( record, 1 )
from
        s = cPickle.dumps(record.__dict__, 1)

And I think this is the way it should be done, both to be 
consistent with the documentation, and to make 
reconstruction on the far end more elegant and intuitive.

----------------------------------------------------------------------

>Comment By: Jason R. Coombs (jaraco)
Date: 2003-06-26 11:45

Message:
Logged In: YES 
user_id=599869

Yes, I think that would be suitable as well, but the 
documentation should be likewise changed to indicate what is 
pickled and how to receive it on the other side.

----------------------------------------------------------------------

Comment By: Vinay Sajip (vsajip)
Date: 2003-06-25 16:02

Message:
Logged In: YES 
user_id=308438

The reason a dict is used, rather than a LogRecord, is that a 
user who receives a logging event as a dict does not have to 
have logging in his/her application (this API predates logging 
coming into the Python distribution). I would suggest leaving 
this as is, but how about if I provide a convenience function 
in logging to unpickle a logrecord, say

def makeLogRecord(dict):
    ""Create and return a LogRecord which has the attributes 
of the specified dictionary".

This meets your valid argument about the kludgy 7-arg call, 
while not breaking existing code and not constraining the 
receiver of a logging event in any particular way.


----------------------------------------------------------------------

Comment By: Raymond Hettinger (rhettinger)
Date: 2003-06-25 13:06

Message:
Logged In: YES 
user_id=80475

Referred to Vinay Sajip.

----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=760703&group_id=5470