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

Skip to content

Commit 40589f4

Browse files
committed
Closes #17795: Reverted backwards-incompatible change in SysLogHandler with Unix domain sockets.
2 parents ca3f294 + e917052 commit 40589f4

2 files changed

Lines changed: 30 additions & 4 deletions

File tree

Lib/logging/handlers.py

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -758,13 +758,17 @@ class SysLogHandler(logging.Handler):
758758
}
759759

760760
def __init__(self, address=('localhost', SYSLOG_UDP_PORT),
761-
facility=LOG_USER, socktype=socket.SOCK_DGRAM):
761+
facility=LOG_USER, socktype=None):
762762
"""
763763
Initialize a handler.
764764
765765
If address is specified as a string, a UNIX socket is used. To log to a
766766
local syslogd, "SysLogHandler(address="/dev/log")" can be used.
767-
If facility is not specified, LOG_USER is used.
767+
If facility is not specified, LOG_USER is used. If socktype is
768+
specified as socket.SOCK_DGRAM or socket.SOCK_STREAM, that specific
769+
socket type will be used. For Unix sockets, you can also specify a
770+
socktype of None, in which case socket.SOCK_DGRAM will be used, falling
771+
back to socket.SOCK_STREAM.
768772
"""
769773
logging.Handler.__init__(self)
770774

@@ -777,18 +781,37 @@ def __init__(self, address=('localhost', SYSLOG_UDP_PORT),
777781
self._connect_unixsocket(address)
778782
else:
779783
self.unixsocket = False
784+
if socktype is None:
785+
socktype = socket.SOCK_DGRAM
780786
self.socket = socket.socket(socket.AF_INET, socktype)
781787
if socktype == socket.SOCK_STREAM:
782788
self.socket.connect(address)
789+
self.socktype = socktype
783790
self.formatter = None
784791

785792
def _connect_unixsocket(self, address):
786-
self.socket = socket.socket(socket.AF_UNIX, self.socktype)
793+
use_socktype = self.socktype
794+
if use_socktype is None:
795+
use_socktype = socket.SOCK_DGRAM
796+
self.socket = socket.socket(socket.AF_UNIX, use_socktype)
787797
try:
788798
self.socket.connect(address)
799+
# it worked, so set self.socktype to the used type
800+
self.socktype = use_socktype
789801
except OSError:
790802
self.socket.close()
791-
raise
803+
if self.socktype is not None:
804+
# user didn't specify falling back, so fail
805+
raise
806+
use_socktype = socket.SOCK_STREAM
807+
self.socket = socket.socket(socket.AF_UNIX, use_socktype)
808+
try:
809+
self.socket.connect(address)
810+
# it worked, so set self.socktype to the used type
811+
self.socktype = use_socktype
812+
except OSError:
813+
self.socket.close()
814+
raise
792815

793816
def encodePriority(self, facility, priority):
794817
"""

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ Core and Builtins
4949
Library
5050
-------
5151

52+
- Issue #17795: Reverted backwards-incompatible change in SysLogHandler with
53+
Unix domain sockets.
54+
5255
- Issue #16694: Add a pure Python implementation of the operator module.
5356
Patch by Zachary Ware.
5457

0 commit comments

Comments
 (0)