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

Skip to content

Commit cbabd7e

Browse files
committed
Issue #7086: Added TCP support to SysLogHandler and tidied up some anachronisms in the code.
1 parent bec4d57 commit cbabd7e

5 files changed

Lines changed: 32 additions & 14 deletions

File tree

Doc/library/logging.rst

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -763,11 +763,13 @@ functions.
763763

764764
Does basic configuration for the logging system by creating a
765765
:class:`StreamHandler` with a default :class:`Formatter` and adding it to the
766-
root logger. The function does nothing if any handlers have been defined for
767-
the root logger. The functions :func:`debug`, :func:`info`, :func:`warning`,
766+
root logger. The functions :func:`debug`, :func:`info`, :func:`warning`,
768767
:func:`error` and :func:`critical` will call :func:`basicConfig` automatically
769768
if no handlers are defined for the root logger.
770769

770+
This function does nothing if the root logger already has handlers
771+
configured for it.
772+
771773
The following keyword arguments are supported.
772774

773775
+--------------+---------------------------------------------+
@@ -1957,16 +1959,22 @@ The :class:`SysLogHandler` class, located in the :mod:`logging.handlers` module,
19571959
supports sending logging messages to a remote or local Unix syslog.
19581960

19591961

1960-
.. class:: SysLogHandler(address=('localhost', SYSLOG_UDP_PORT), facility=LOG_USER)
1962+
.. class:: SysLogHandler(address=('localhost', SYSLOG_UDP_PORT), facility=LOG_USER, socktype=socket.SOCK_DGRAM)
19611963

19621964
Returns a new instance of the :class:`SysLogHandler` class intended to
19631965
communicate with a remote Unix machine whose address is given by *address* in
19641966
the form of a ``(host, port)`` tuple. If *address* is not specified,
1965-
``('localhost', 514)`` is used. The address is used to open a UDP socket. An
1967+
``('localhost', 514)`` is used. The address is used to open a socket. An
19661968
alternative to providing a ``(host, port)`` tuple is providing an address as a
19671969
string, for example "/dev/log". In this case, a Unix domain socket is used to
19681970
send the message to the syslog. If *facility* is not specified,
1969-
:const:`LOG_USER` is used.
1971+
:const:`LOG_USER` is used. The type of socket opened depends on the
1972+
*socktype* argument, which defaults to :const:`socket.SOCK_DGRAM` and thus
1973+
opens a UDP socket. To open a TCP socket (for use with the newer syslog
1974+
daemons such as rsyslog), specify a value of :const:`socket.SOCK_STREAM`.
1975+
1976+
.. versionchanged:: 3.2
1977+
*socktype* was added.
19701978

19711979

19721980
.. method:: close()

Lib/logging/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@
4646

4747
__author__ = "Vinay Sajip <[email protected]>"
4848
__status__ = "production"
49-
__version__ = "0.5.0.7"
50-
__date__ = "20 January 2009"
49+
__version__ = "0.5.0.9"
50+
__date__ = "09 October 2009"
5151

5252
#---------------------------------------------------------------------------
5353
# Miscellaneous module data

Lib/logging/config.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
is based on PEP 282 and comments thereto in comp.lang.python, and influenced
2020
by Apache's log4j system.
2121
22-
Copyright (C) 2001-2008 Vinay Sajip. All Rights Reserved.
22+
Copyright (C) 2001-2009 Vinay Sajip. All Rights Reserved.
2323
2424
To use, simply 'import logging' and log away!
2525
"""

Lib/logging/handlers.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2001-2007 by Vinay Sajip. All Rights Reserved.
1+
# Copyright 2001-2009 by Vinay Sajip. All Rights Reserved.
22
#
33
# Permission to use, copy, modify, and distribute this software and its
44
# documentation for any purpose and without fee is hereby granted,
@@ -41,6 +41,7 @@
4141
DEFAULT_HTTP_LOGGING_PORT = 9022
4242
DEFAULT_SOAP_LOGGING_PORT = 9023
4343
SYSLOG_UDP_PORT = 514
44+
SYSLOG_TCP_PORT = 514
4445

4546
_MIDNIGHT = 24 * 60 * 60 # number of seconds in a day
4647

@@ -155,7 +156,7 @@ class TimedRotatingFileHandler(BaseRotatingHandler):
155156
If backupCount is > 0, when rollover is done, no more than backupCount
156157
files are kept - the oldest ones are deleted.
157158
"""
158-
def __init__(self, filename, when='h', interval=1, backupCount=0, encoding=None, delay=0, utc=False):
159+
def __init__(self, filename, when='h', interval=1, backupCount=0, encoding=None, delay=False, utc=False):
159160
BaseRotatingHandler.__init__(self, filename, 'a', encoding, delay)
160161
self.when = when.upper()
161162
self.backupCount = backupCount
@@ -690,7 +691,8 @@ class SysLogHandler(logging.Handler):
690691
"CRITICAL" : "critical"
691692
}
692693

693-
def __init__(self, address=('localhost', SYSLOG_UDP_PORT), facility=LOG_USER):
694+
def __init__(self, address=('localhost', SYSLOG_UDP_PORT),
695+
facility=LOG_USER, socktype=socket.SOCK_DGRAM):
694696
"""
695697
Initialize a handler.
696698
@@ -702,13 +704,16 @@ def __init__(self, address=('localhost', SYSLOG_UDP_PORT), facility=LOG_USER):
702704

703705
self.address = address
704706
self.facility = facility
707+
self.socktype = socktype
708+
705709
if isinstance(address, str):
706710
self.unixsocket = 1
707711
self._connect_unixsocket(address)
708712
else:
709713
self.unixsocket = 0
710-
self.socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
711-
714+
self.socket = socket.socket(socket.AF_INET, socktype)
715+
if socktype == socket.SOCK_STREAM:
716+
self.socket.connect(address)
712717
self.formatter = None
713718

714719
def _connect_unixsocket(self, address):
@@ -781,8 +786,10 @@ def emit(self, record):
781786
except socket.error:
782787
self._connect_unixsocket(self.address)
783788
self.socket.send(msg)
784-
else:
789+
elif self.socktype == socket.SOCK_DGRAM:
785790
self.socket.sendto(msg, self.address)
791+
else:
792+
self.socket.sendall(msg)
786793
except (KeyboardInterrupt, SystemExit):
787794
raise
788795
except:

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,9 @@ C-API
8787
Library
8888
-------
8989

90+
- Issue #7086: Added TCP support to SysLogHandler, and tidied up some
91+
anachronisms in the code which were a relic of 1.5.2 compatibility.
92+
9093
- Issue #7082: When falling back to the MIME 'name' parameter, the
9194
correct place to look for it is the Content-Type header.
9295

0 commit comments

Comments
 (0)