Back to index

python3.2  3.2.2
Public Member Functions | Public Attributes
logging.handlers.SocketHandler Class Reference
Inheritance diagram for logging.handlers.SocketHandler:
Inheritance graph
[legend]

List of all members.

Public Member Functions

def __init__
def makeSocket
def createSocket
def send
def makePickle
def handleError
def emit
def close

Public Attributes

 host
 port
 sock
 closeOnError
 retryTime
 retryStart
 retryMax
 retryFactor
 retryPeriod

Detailed Description

A handler class which writes logging records, in pickle format, to
a streaming socket. The socket is kept open across logging calls.
If the peer resets it, an attempt is made to reconnect on the next call.
The pickle which is sent is that of the LogRecord's attribute dictionary
(__dict__), so that the receiver does not need to have the logging module
installed in order to process the logging event.

To unpickle the record at the receiving end into a LogRecord, use the
makeLogRecord function.

Definition at line 407 of file handlers.py.


Constructor & Destructor Documentation

def logging.handlers.SocketHandler.__init__ (   self,
  host,
  port 
)
Initializes the handler with a specific host address and port.

The attribute 'closeOnError' is set to 1 - which means that if
a socket error occurs, the socket is silently closed and then
reopened on the next logging call.

Reimplemented in logging.handlers.DatagramHandler.

Definition at line 420 of file handlers.py.

00420 
00421     def __init__(self, host, port):
00422         """
00423         Initializes the handler with a specific host address and port.
00424 
00425         The attribute 'closeOnError' is set to 1 - which means that if
00426         a socket error occurs, the socket is silently closed and then
00427         reopened on the next logging call.
00428         """
00429         logging.Handler.__init__(self)
00430         self.host = host
00431         self.port = port
00432         self.sock = None
00433         self.closeOnError = 0
00434         self.retryTime = None
00435         #
00436         # Exponential backoff parameters.
00437         #
00438         self.retryStart = 1.0
00439         self.retryMax = 30.0
00440         self.retryFactor = 2.0

Here is the caller graph for this function:


Member Function Documentation

Closes the socket.

Definition at line 553 of file handlers.py.

00553 
00554     def close(self):
00555         """
00556         Closes the socket.
00557         """
00558         if self.sock:
00559             self.sock.close()
00560             self.sock = None
00561         logging.Handler.close(self)

Here is the caller graph for this function:

Try to create a socket, using an exponential backoff with
a max retry time. Thanks to Robert Olson for the original patch
(SF #815911) which has been slightly refactored.

Definition at line 452 of file handlers.py.

00452 
00453     def createSocket(self):
00454         """
00455         Try to create a socket, using an exponential backoff with
00456         a max retry time. Thanks to Robert Olson for the original patch
00457         (SF #815911) which has been slightly refactored.
00458         """
00459         now = time.time()
00460         # Either retryTime is None, in which case this
00461         # is the first time back after a disconnect, or
00462         # we've waited long enough.
00463         if self.retryTime is None:
00464             attempt = 1
00465         else:
00466             attempt = (now >= self.retryTime)
00467         if attempt:
00468             try:
00469                 self.sock = self.makeSocket()
00470                 self.retryTime = None # next time, no delay before trying
00471             except socket.error:
00472                 #Creation failed, so set the retry time and return.
00473                 if self.retryTime is None:
00474                     self.retryPeriod = self.retryStart
00475                 else:
00476                     self.retryPeriod = self.retryPeriod * self.retryFactor
00477                     if self.retryPeriod > self.retryMax:
00478                         self.retryPeriod = self.retryMax
00479                 self.retryTime = now + self.retryPeriod

Here is the call graph for this function:

Here is the caller graph for this function:

def logging.handlers.SocketHandler.emit (   self,
  record 
)
Emit a record.

Pickles the record and writes it to the socket in binary format.
If there is an error with the socket, silently drop the packet.
If there was a problem with the socket, re-establishes the
socket.

Definition at line 536 of file handlers.py.

00536 
00537     def emit(self, record):
00538         """
00539         Emit a record.
00540 
00541         Pickles the record and writes it to the socket in binary format.
00542         If there is an error with the socket, silently drop the packet.
00543         If there was a problem with the socket, re-establishes the
00544         socket.
00545         """
00546         try:
00547             s = self.makePickle(record)
00548             self.send(s)
00549         except (KeyboardInterrupt, SystemExit):
00550             raise
00551         except:
00552             self.handleError(record)

Here is the call graph for this function:

def logging.handlers.SocketHandler.handleError (   self,
  record 
)
Handle an error during logging.

An error has occurred during logging. Most likely cause -
connection lost. Close the socket so that we can retry on the
next event.

Definition at line 522 of file handlers.py.

00522 
00523     def handleError(self, record):
00524         """
00525         Handle an error during logging.
00526 
00527         An error has occurred during logging. Most likely cause -
00528         connection lost. Close the socket so that we can retry on the
00529         next event.
00530         """
00531         if self.closeOnError and self.sock:
00532             self.sock.close()
00533             self.sock = None        #try to reconnect next time
00534         else:
00535             logging.Handler.handleError(self, record)

Here is the caller graph for this function:

def logging.handlers.SocketHandler.makePickle (   self,
  record 
)
Pickles the record in binary format with a length prefix, and
returns it ready for transmission across the socket.

Definition at line 507 of file handlers.py.

00507 
00508     def makePickle(self, record):
00509         """
00510         Pickles the record in binary format with a length prefix, and
00511         returns it ready for transmission across the socket.
00512         """
00513         ei = record.exc_info
00514         if ei:
00515             dummy = self.format(record) # just to get traceback text into record.exc_text
00516             record.exc_info = None  # to avoid Unpickleable error
00517         s = pickle.dumps(record.__dict__, 1)
00518         if ei:
00519             record.exc_info = ei  # for next handler
00520         slen = struct.pack(">L", len(s))
00521         return slen + s

Here is the call graph for this function:

Here is the caller graph for this function:

def logging.handlers.SocketHandler.makeSocket (   self,
  timeout = 1 
)
A factory method which allows subclasses to define the precise
type of socket they want.

Definition at line 441 of file handlers.py.

00441 
00442     def makeSocket(self, timeout=1):
00443         """
00444         A factory method which allows subclasses to define the precise
00445         type of socket they want.
00446         """
00447         s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
00448         if hasattr(s, 'settimeout'):
00449             s.settimeout(timeout)
00450         s.connect((self.host, self.port))
00451         return s

Here is the caller graph for this function:

def logging.handlers.SocketHandler.send (   self,
  s 
)
Send a pickled string to the socket.

This function allows for partial sends which can happen when the
network is busy.

Reimplemented in logging.handlers.DatagramHandler.

Definition at line 480 of file handlers.py.

00480 
00481     def send(self, s):
00482         """
00483         Send a pickled string to the socket.
00484 
00485         This function allows for partial sends which can happen when the
00486         network is busy.
00487         """
00488         if self.sock is None:
00489             self.createSocket()
00490         #self.sock can be None either because we haven't reached the retry
00491         #time yet, or because we have reached the retry time and retried,
00492         #but are still unable to connect.
00493         if self.sock:
00494             try:
00495                 if hasattr(self.sock, "sendall"):
00496                     self.sock.sendall(s)
00497                 else:
00498                     sentsofar = 0
00499                     left = len(s)
00500                     while left > 0:
00501                         sent = self.sock.send(s[sentsofar:])
00502                         sentsofar = sentsofar + sent
00503                         left = left - sent
00504             except socket.error:
00505                 self.sock.close()
00506                 self.sock = None  # so we can call createSocket next time

Here is the call graph for this function:

Here is the caller graph for this function:


Member Data Documentation

Reimplemented in logging.handlers.DatagramHandler.

Definition at line 432 of file handlers.py.

Definition at line 429 of file handlers.py.

Definition at line 430 of file handlers.py.

Definition at line 439 of file handlers.py.

Definition at line 438 of file handlers.py.

Definition at line 473 of file handlers.py.

Definition at line 437 of file handlers.py.

Definition at line 433 of file handlers.py.

Definition at line 431 of file handlers.py.


The documentation for this class was generated from the following file: