mirror of
https://github.com/python/cpython.git
synced 2025-08-04 17:08:35 +00:00
bpo-44291: Fix reconnection in logging.handlers.SysLogHandler (GH-26490)
This commit is contained in:
parent
8f010dc920
commit
3d315c3116
2 changed files with 46 additions and 25 deletions
|
@ -835,6 +835,36 @@ class SysLogHandler(logging.Handler):
|
|||
self.address = address
|
||||
self.facility = facility
|
||||
self.socktype = socktype
|
||||
self.socket = None
|
||||
self.createSocket()
|
||||
|
||||
def _connect_unixsocket(self, address):
|
||||
use_socktype = self.socktype
|
||||
if use_socktype is None:
|
||||
use_socktype = socket.SOCK_DGRAM
|
||||
self.socket = socket.socket(socket.AF_UNIX, use_socktype)
|
||||
try:
|
||||
self.socket.connect(address)
|
||||
# it worked, so set self.socktype to the used type
|
||||
self.socktype = use_socktype
|
||||
except OSError:
|
||||
self.socket.close()
|
||||
if self.socktype is not None:
|
||||
# user didn't specify falling back, so fail
|
||||
raise
|
||||
use_socktype = socket.SOCK_STREAM
|
||||
self.socket = socket.socket(socket.AF_UNIX, use_socktype)
|
||||
try:
|
||||
self.socket.connect(address)
|
||||
# it worked, so set self.socktype to the used type
|
||||
self.socktype = use_socktype
|
||||
except OSError:
|
||||
self.socket.close()
|
||||
raise
|
||||
|
||||
def createSocket(self):
|
||||
address = self.address
|
||||
socktype = self.socktype
|
||||
|
||||
if isinstance(address, str):
|
||||
self.unixsocket = True
|
||||
|
@ -871,30 +901,6 @@ class SysLogHandler(logging.Handler):
|
|||
self.socket = sock
|
||||
self.socktype = socktype
|
||||
|
||||
def _connect_unixsocket(self, address):
|
||||
use_socktype = self.socktype
|
||||
if use_socktype is None:
|
||||
use_socktype = socket.SOCK_DGRAM
|
||||
self.socket = socket.socket(socket.AF_UNIX, use_socktype)
|
||||
try:
|
||||
self.socket.connect(address)
|
||||
# it worked, so set self.socktype to the used type
|
||||
self.socktype = use_socktype
|
||||
except OSError:
|
||||
self.socket.close()
|
||||
if self.socktype is not None:
|
||||
# user didn't specify falling back, so fail
|
||||
raise
|
||||
use_socktype = socket.SOCK_STREAM
|
||||
self.socket = socket.socket(socket.AF_UNIX, use_socktype)
|
||||
try:
|
||||
self.socket.connect(address)
|
||||
# it worked, so set self.socktype to the used type
|
||||
self.socktype = use_socktype
|
||||
except OSError:
|
||||
self.socket.close()
|
||||
raise
|
||||
|
||||
def encodePriority(self, facility, priority):
|
||||
"""
|
||||
Encode the facility and priority. You can pass in strings or
|
||||
|
@ -914,7 +920,10 @@ class SysLogHandler(logging.Handler):
|
|||
"""
|
||||
self.acquire()
|
||||
try:
|
||||
self.socket.close()
|
||||
sock = self.socket
|
||||
if sock:
|
||||
self.socket = None
|
||||
sock.close()
|
||||
logging.Handler.close(self)
|
||||
finally:
|
||||
self.release()
|
||||
|
@ -954,6 +963,10 @@ class SysLogHandler(logging.Handler):
|
|||
# Message is a string. Convert to bytes as required by RFC 5424
|
||||
msg = msg.encode('utf-8')
|
||||
msg = prio + msg
|
||||
|
||||
if not self.socket:
|
||||
self.createSocket()
|
||||
|
||||
if self.unixsocket:
|
||||
try:
|
||||
self.socket.send(msg)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue