Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6f3eaa6

Browse files
committed
SF patch #761519: Fixes for bugs 760703 and 757821
SF bug #760703: SocketHandler and LogRecord don't work well together SF bug #757821: logging module docs Applied Vinay Sajip's patch with a few minor fixups and a NEWS item. Patched __init__.py - added new function makeLogRecord (for bug report 760703). Patched handlers.py - updated some docstrings and deleted some old commented-out code. Patched test_logging.py to make use of makeLogRecord. Patched liblogging.tex to fill documentation gaps (both 760703 and bug 757821).
1 parent 6ebe61f commit 6f3eaa6

5 files changed

Lines changed: 59 additions & 32 deletions

File tree

Doc/lib/liblogging.tex

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ \section{\module{logging} ---
5252
\class{Handler} class. Handlers are responsible for ensuring that a logged
5353
message (in the form of a \class{LogRecord}) ends up in a particular
5454
location (or set of locations) which is useful for the target audience for
55-
that message (e.g. end users, support desk staff, system administrators,
55+
that message (such as end users, support desk staff, system administrators,
5656
developers). Handlers are passed \class{LogRecord} instances intended for
5757
particular destinations. Each logger can have zero, one or more handlers
5858
associated with it (via the \method{addHandler} method of \class{Logger}).
@@ -204,6 +204,13 @@ \section{\module{logging} ---
204204
returned.
205205
\end{funcdesc}
206206

207+
\begin{funcdesc}{makeLogRecord}{attrdict}
208+
Creates and returns a new \class{LogRecord} instance whose attributes are
209+
defined by \var{attrdict}. This function is useful for taking a pickled
210+
\class{LogRecord} attribute dictionary, sent over a socket, and reconstituting
211+
it as a \class{LogRecord} instance at the receiving end.
212+
\end{funcdesc}
213+
207214
\begin{funcdesc}{basicConfig}{}
208215
Does basic configuration for the logging system by creating a
209216
\class{StreamHandler} with a default \class{Formatter} and adding it to
@@ -266,7 +273,7 @@ \subsection{Logger Objects}
266273
Indicates the effective level for this logger. If a value other than
267274
\constant{NOTSET} has been set using \method{setLevel()}, it is returned.
268275
Otherwise, the hierarchy is traversed towards the root until a value
269-
other than \constant{NOTSET} is found,and that value is returned.
276+
other than \constant{NOTSET} is found, and that value is returned.
270277
\end{methoddesc}
271278

272279
\begin{methoddesc}{debug}{msg\optional{, *args\optional{, **kwargs}}}
@@ -535,9 +542,11 @@ \subsubsection{SocketHandler}
535542
\end{methoddesc}
536543

537544
\begin{methoddesc}{emit}{}
538-
Pickles the record and writes it to the socket in binary format.
539-
If there is an error with the socket, silently drops the packet.
540-
If the connection was previously lost, re-establishes the connection.
545+
Pickles the record's attribute dictionary and writes it to the socket in
546+
binary format. If there is an error with the socket, silently drops the
547+
packet. If the connection was previously lost, re-establishes the connection.
548+
To unpickle the record at the receiving end into a LogRecord, use the
549+
\function{makeLogRecord} function.
541550
\end{methoddesc}
542551

543552
\begin{methoddesc}{handleError}{}
@@ -553,8 +562,8 @@ \subsubsection{SocketHandler}
553562
\end{methoddesc}
554563

555564
\begin{methoddesc}{makePickle}{record}
556-
Pickles the record in binary format with a length prefix, and returns
557-
it ready for transmission across the socket.
565+
Pickles the record's attribute dictionary in binary format with a length
566+
prefix, and returns it ready for transmission across the socket.
558567
\end{methoddesc}
559568

560569
\begin{methoddesc}{send}{packet}
@@ -574,8 +583,11 @@ \subsubsection{DatagramHandler}
574583
\end{classdesc}
575584

576585
\begin{methoddesc}{emit}{}
577-
Pickles the record and writes it to the socket in binary format.
578-
If there is an error with the socket, silently drops the packet.
586+
Pickles the record's attribute dictionary and writes it to the socket in
587+
binary format. If there is an error with the socket, silently drops the
588+
packet.
589+
To unpickle the record at the receiving end into a LogRecord, use the
590+
\function{makeLogRecord} function.
579591
\end{methoddesc}
580592

581593
\begin{methoddesc}{makeSocket}{}
@@ -584,8 +596,7 @@ \subsubsection{DatagramHandler}
584596
\end{methoddesc}
585597

586598
\begin{methoddesc}{send}{s}
587-
Send a pickled string to a socket. This function allows for
588-
partial sends which can happen when the network is busy.
599+
Send a pickled string to a socket.
589600
\end{methoddesc}
590601

591602
\subsubsection{SysLogHandler}
@@ -789,8 +800,8 @@ \subsection{Formatter Objects}
789800
supplied, the default value of "\%s(message)\\n" is used.
790801

791802
A Formatter can be initialized with a format string which makes use of
792-
knowledge of the \class{LogRecord} attributes - e.g. the default value
793-
mentioned above makes use of the fact that the user's message and
803+
knowledge of the \class{LogRecord} attributes - such as the default value
804+
mentioned above making use of the fact that the user's message and
794805
arguments are pre- formatted into a LogRecord's \var{message}
795806
attribute. Currently, the useful attributes in a LogRecord are
796807
described by:
@@ -916,7 +927,11 @@ \subsection{Configuration}
916927

917928
\subsubsection{Configuration functions}
918929

919-
The following functions allow the logging module to be configured.
930+
The following functions allow the logging module to be configured. Before
931+
they can be used, you must import \module{logging.config}. Their use is optional -
932+
you can configure the logging module entirely by making calls to the main
933+
API (defined in \module{logging} itself) and defining handlers which are declared
934+
either in \module{logging} or \module{logging.handlers}.
920935

921936
\begin{funcdesc}{fileConfig}{fname\optional{, defaults}}
922937
Reads the logging configuration from a ConfigParser-format file named
@@ -1013,7 +1028,7 @@ \subsubsection{Configuration file format}
10131028
higher up the logger hierarchy from this logger, or 0 to indicate that
10141029
messages are \strong{not} propagated to handlers up the hierarchy. The
10151030
\code{qualname} entry is the hierarchical channel name of the logger,
1016-
i.e. the name used by the application to get the logger.
1031+
for example, the name used by the application to get the logger.
10171032

10181033
Sections which specify handler configuration are exemplified by the
10191034
following.

Lib/logging/__init__.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@
3636

3737
__author__ = "Vinay Sajip <[email protected]>"
3838
__status__ = "beta"
39-
__version__ = "0.4.8"
40-
__date__ = "22 April 2003"
39+
__version__ = "0.4.8.1"
40+
__date__ = "26 June 2003"
4141

4242
#---------------------------------------------------------------------------
4343
# Miscellaneous module data
@@ -233,6 +233,17 @@ def getMessage(self):
233233
msg = msg % self.args
234234
return msg
235235

236+
def makeLogRecord(dict):
237+
"""
238+
Make a LogRecord whose attributes are defined by the specified dictionary,
239+
This function is useful for converting a logging event received over
240+
a socket connection (which is sent as a dictionary) into a LogRecord
241+
instance.
242+
"""
243+
rv = LogRecord(None, None, "", 0, "", (), None)
244+
rv.__dict__.update(dict)
245+
return rv
246+
236247
#---------------------------------------------------------------------------
237248
# Formatter classes and functions
238249
#---------------------------------------------------------------------------

Lib/logging/handlers.py

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,12 @@ class SocketHandler(logging.Handler):
111111
A handler class which writes logging records, in pickle format, to
112112
a streaming socket. The socket is kept open across logging calls.
113113
If the peer resets it, an attempt is made to reconnect on the next call.
114-
Note that the very simple wire protocol used means that packet sizes
115-
are expected to be encodable within 16 bits (i.e. < 32767 bytes).
114+
The pickle which is sent is that of the LogRecord's attribute dictionary
115+
(__dict__), so that the receiver does not need to have the logging module
116+
installed in order to process the logging event.
117+
118+
To unpickle the record at the receiving end into a LogRecord, use the
119+
makeLogRecord function.
116120
"""
117121

118122
def __init__(self, host, port):
@@ -208,9 +212,12 @@ def close(self):
208212
class DatagramHandler(SocketHandler):
209213
"""
210214
A handler class which writes logging records, in pickle format, to
211-
a datagram socket. Note that the very simple wire protocol used means
212-
that packet sizes are expected to be encodable within 16 bits
213-
(i.e. < 32767 bytes).
215+
a datagram socket. The pickle which is sent is that of the LogRecord's
216+
attribute dictionary (__dict__), so that the receiver does not need to
217+
have the logging module installed in order to process the logging event.
218+
219+
To unpickle the record at the receiving end into a LogRecord, use the
220+
makeLogRecord function.
214221
215222
"""
216223
def __init__(self, host, port):
@@ -236,14 +243,6 @@ def send(self, s):
236243
when the network is busy - UDP does not guarantee delivery and
237244
can deliver packets out of sequence.
238245
"""
239-
#old code
240-
#sentsofar = 0
241-
#left = len(s)
242-
#addr = (self.host, self.port)
243-
#while left > 0:
244-
# sent = self.sock.sendto(s[sentsofar:], addr)
245-
# sentsofar = sentsofar + sent
246-
# left = left - sent
247246
self.sock.sendto(s, (self.host, self.port))
248247

249248
class SysLogHandler(logging.Handler):

Lib/test/test_logging.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,7 @@ def handle(self):
7070
while len(chunk) < slen:
7171
chunk = chunk + self.connection.recv(slen - len(chunk))
7272
obj = self.unPickle(chunk)
73-
record = logging.LogRecord(None, None, "", 0, "", (), None)
74-
record.__dict__.update(obj)
73+
record = logging.makeLogRecord(obj)
7574
self.handleLogRecord(record)
7675
except:
7776
raise

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,9 @@ Extension modules
8383
Library
8484
-------
8585

86+
- The logging module now has a new function, makeLogRecord() making
87+
LogHandler easier to interact with DatagramHandler and SocketHandler.
88+
8689
- For compatibility with doctests created before 2.3, if an expected
8790
output block consists solely of "1" and the actual output block
8891
consists solely of "True", it's accepted as a match; similarly

0 commit comments

Comments
 (0)