| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -613,7 +613,7 @@ The :class:`SysLogHandler` class, located in the :mod:`logging.handlers` module, | |||
| 613 | 613 | supports sending logging messages to a remote or local Unix syslog. | |
| 614 | 614 | ||
| 615 | 615 | ||
| 616 | - .. class:: SysLogHandler(address=('localhost', SYSLOG_UDP_PORT), facility=LOG_USER, socktype=socket.SOCK_DGRAM) | ||
| 616 | + .. class:: SysLogHandler(address=('localhost', SYSLOG_UDP_PORT), facility=LOG_USER, socktype=socket.SOCK_DGRAM, timeout=None) | ||
| 617 | 617 | ||
| 618 | 618 | Returns a new instance of the :class:`SysLogHandler` class intended to | |
| 619 | 619 | communicate with a remote Unix machine whose address is given by *address* in | |
@@ -626,6 +626,11 @@ supports sending logging messages to a remote or local Unix syslog. | |||
| 626 | 626 | *socktype* argument, which defaults to :const:`socket.SOCK_DGRAM` and thus | |
| 627 | 627 | opens a UDP socket. To open a TCP socket (for use with the newer syslog | |
| 628 | 628 | daemons such as rsyslog), specify a value of :const:`socket.SOCK_STREAM`. | |
| 629 | + If *timeout* is specified, it sets a timeout (in seconds) for the socket operations. | ||
| 630 | + This can help prevent the program from hanging indefinitely if the syslog server is | ||
| 631 | + unreachable. By default, *timeout* is ``None``, meaning no timeout is applied. | ||
| 632 | + | ||
| 633 | + | ||
| 629 | 634 | ||
| 630 | 635 | Note that if your server is not listening on UDP port 514, | |
| 631 | 636 | :class:`SysLogHandler` may appear not to work. In that case, check what | |
@@ -645,6 +650,8 @@ supports sending logging messages to a remote or local Unix syslog. | |||
| 645 | 650 | .. versionchanged:: 3.2 | |
| 646 | 651 | *socktype* was added. | |
| 647 | 652 | ||
| 653 | + .. versionchanged:: 3.14 | ||
| 654 | + *timeout* was added. | ||
| 648 | 655 | ||
| 649 | 656 | .. method:: close() | |
| 650 | 657 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -855,7 +855,7 @@ class SysLogHandler(logging.Handler): | |||
| 855 | 855 | } | |
| 856 | 856 | ||
| 857 | 857 | def __init__(self, address=('localhost', SYSLOG_UDP_PORT), | |
| 858 | - facility=LOG_USER, socktype=None): | ||
| 858 | + facility=LOG_USER, socktype=None, timeout=None): | ||
| 859 | 859 | """ | |
| 860 | 860 | Initialize a handler. | |
| 861 | 861 | ||
@@ -872,6 +872,7 @@ def __init__(self, address=('localhost', SYSLOG_UDP_PORT), | |||
| 872 | 872 | self.address = address | |
| 873 | 873 | self.facility = facility | |
| 874 | 874 | self.socktype = socktype | |
| 875 | + self.timeout = timeout | ||
| 875 | 876 | self.socket = None | |
| 876 | 877 | self.createSocket() | |
| 877 | 878 | ||
@@ -933,6 +934,8 @@ def createSocket(self): | |||
| 933 | 934 | err = sock = None | |
| 934 | 935 | try: | |
| 935 | 936 | sock = socket.socket(af, socktype, proto) | |
| 937 | + if self.timeout: | ||
| 938 | + sock.settimeout(self.timeout) | ||
| 936 | 939 | if socktype == socket.SOCK_STREAM: | |
| 937 | 940 | sock.connect(sa) | |
| 938 | 941 | break | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -22,6 +22,7 @@ | |||
| 22 | 22 | import logging.handlers | |
| 23 | 23 | import logging.config | |
| 24 | 24 | ||
| 25 | + | ||
| 25 | 26 | import codecs | |
| 26 | 27 | import configparser | |
| 27 | 28 | import copy | |
@@ -2095,6 +2096,18 @@ def test_udp_reconnection(self): | |||
| 2095 | 2096 | self.handled.wait(support.LONG_TIMEOUT) | |
| 2096 | 2097 | self.assertEqual(self.log_output, b'<11>sp\xc3\xa4m\x00') | |
| 2097 | 2098 | ||
| 2099 | + @patch('socket.socket') | ||
| 2100 | + def test_tcp_timeout(self, mock_socket): | ||
| 2101 | + instance_mock_sock = mock_socket.return_value | ||
| 2102 | + instance_mock_sock.connect.side_effect = socket.timeout | ||
| 2103 | + | ||
| 2104 | + with self.assertRaises(socket.timeout): | ||
| 2105 | + logging.handlers.SysLogHandler(address=('localhost', 514), | ||
| 2106 | + socktype=socket.SOCK_STREAM, | ||
| 2107 | + timeout=1) | ||
| 2108 | + | ||
| 2109 | + instance_mock_sock.close.assert_called() | ||
| 2110 | + | ||
| 2098 | 2111 | @unittest.skipUnless(hasattr(socket, "AF_UNIX"), "Unix sockets required") | |
| 2099 | 2112 | class UnixSysLogHandlerTest(SysLogHandlerTest): | |
| 2100 | 2113 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,2 @@ | |||
| 1 | + Add a socket *timeout* keyword argument to | ||
| 2 | + :class:`logging.handlers.SysLogHandler`. | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments