Merge "py36: Fix syslog fallback to UDP"
This commit is contained in:
commit
354a86a80e
@ -2373,13 +2373,19 @@ def get_logger(conf, name=None, log_to_console=False, log_route=None,
|
|||||||
facility=facility)
|
facility=facility)
|
||||||
else:
|
else:
|
||||||
log_address = conf.get('log_address', '/dev/log')
|
log_address = conf.get('log_address', '/dev/log')
|
||||||
|
handler = None
|
||||||
try:
|
try:
|
||||||
handler = ThreadSafeSysLogHandler(address=log_address,
|
mode = os.stat(log_address).st_mode
|
||||||
facility=facility)
|
if stat.S_ISSOCK(mode):
|
||||||
except socket.error as e:
|
handler = ThreadSafeSysLogHandler(address=log_address,
|
||||||
# Either /dev/log isn't a UNIX socket or it does not exist at all
|
facility=facility)
|
||||||
|
except (OSError, socket.error) as e:
|
||||||
|
# If either /dev/log isn't a UNIX socket or it does not exist at
|
||||||
|
# all then py2 would raise an error
|
||||||
if e.errno not in [errno.ENOTSOCK, errno.ENOENT]:
|
if e.errno not in [errno.ENOTSOCK, errno.ENOENT]:
|
||||||
raise
|
raise
|
||||||
|
if handler is None:
|
||||||
|
# fallback to default UDP
|
||||||
handler = ThreadSafeSysLogHandler(facility=facility)
|
handler = ThreadSafeSysLogHandler(facility=facility)
|
||||||
handler.setFormatter(formatter)
|
handler.setFormatter(formatter)
|
||||||
logger.addHandler(handler)
|
logger.addHandler(handler)
|
||||||
|
@ -1752,7 +1752,8 @@ class TestUtils(unittest.TestCase):
|
|||||||
self.assertEqual(sio.getvalue(),
|
self.assertEqual(sio.getvalue(),
|
||||||
'test1\ntest3\ntest4\ntest6\n')
|
'test1\ntest3\ntest4\ntest6\n')
|
||||||
|
|
||||||
def test_get_logger_sysloghandler_plumbing(self):
|
@with_tempdir
|
||||||
|
def test_get_logger_sysloghandler_plumbing(self, tempdir):
|
||||||
orig_sysloghandler = utils.ThreadSafeSysLogHandler
|
orig_sysloghandler = utils.ThreadSafeSysLogHandler
|
||||||
syslog_handler_args = []
|
syslog_handler_args = []
|
||||||
|
|
||||||
@ -1773,6 +1774,7 @@ class TestUtils(unittest.TestCase):
|
|||||||
with mock.patch.object(utils, 'ThreadSafeSysLogHandler',
|
with mock.patch.object(utils, 'ThreadSafeSysLogHandler',
|
||||||
syslog_handler_catcher), \
|
syslog_handler_catcher), \
|
||||||
mock.patch.object(socket, 'getaddrinfo', fake_getaddrinfo):
|
mock.patch.object(socket, 'getaddrinfo', fake_getaddrinfo):
|
||||||
|
# default log_address
|
||||||
utils.get_logger({
|
utils.get_logger({
|
||||||
'log_facility': 'LOG_LOCAL3',
|
'log_facility': 'LOG_LOCAL3',
|
||||||
}, 'server', log_route='server')
|
}, 'server', log_route='server')
|
||||||
@ -1783,19 +1785,51 @@ class TestUtils(unittest.TestCase):
|
|||||||
os.path.isdir('/dev/log'):
|
os.path.isdir('/dev/log'):
|
||||||
# Since socket on OSX is in /var/run/syslog, there will be
|
# Since socket on OSX is in /var/run/syslog, there will be
|
||||||
# a fallback to UDP.
|
# a fallback to UDP.
|
||||||
expected_args.append(
|
expected_args = [
|
||||||
((), {'facility': orig_sysloghandler.LOG_LOCAL3}))
|
((), {'facility': orig_sysloghandler.LOG_LOCAL3})]
|
||||||
self.assertEqual(expected_args, syslog_handler_args)
|
self.assertEqual(expected_args, syslog_handler_args)
|
||||||
|
|
||||||
|
# custom log_address - file doesn't exist: fallback to UDP
|
||||||
|
log_address = os.path.join(tempdir, 'foo')
|
||||||
syslog_handler_args = []
|
syslog_handler_args = []
|
||||||
utils.get_logger({
|
utils.get_logger({
|
||||||
'log_facility': 'LOG_LOCAL3',
|
'log_facility': 'LOG_LOCAL3',
|
||||||
'log_address': '/foo/bar',
|
'log_address': log_address,
|
||||||
}, 'server', log_route='server')
|
}, 'server', log_route='server')
|
||||||
|
expected_args = [
|
||||||
|
((), {'facility': orig_sysloghandler.LOG_LOCAL3})]
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
((), {'address': '/foo/bar',
|
expected_args, syslog_handler_args)
|
||||||
'facility': orig_sysloghandler.LOG_LOCAL3}),
|
|
||||||
syslog_handler_args[0])
|
# custom log_address - file exists, not a socket: fallback to UDP
|
||||||
|
with open(log_address, 'w'):
|
||||||
|
pass
|
||||||
|
syslog_handler_args = []
|
||||||
|
utils.get_logger({
|
||||||
|
'log_facility': 'LOG_LOCAL3',
|
||||||
|
'log_address': log_address,
|
||||||
|
}, 'server', log_route='server')
|
||||||
|
expected_args = [
|
||||||
|
((), {'facility': orig_sysloghandler.LOG_LOCAL3})]
|
||||||
|
self.assertEqual(
|
||||||
|
expected_args, syslog_handler_args)
|
||||||
|
|
||||||
|
# custom log_address - file exists, is a socket: use it
|
||||||
|
os.unlink(log_address)
|
||||||
|
with contextlib.closing(
|
||||||
|
socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM)) as sock:
|
||||||
|
sock.settimeout(5)
|
||||||
|
sock.bind(log_address)
|
||||||
|
syslog_handler_args = []
|
||||||
|
utils.get_logger({
|
||||||
|
'log_facility': 'LOG_LOCAL3',
|
||||||
|
'log_address': log_address,
|
||||||
|
}, 'server', log_route='server')
|
||||||
|
expected_args = [
|
||||||
|
((), {'address': log_address,
|
||||||
|
'facility': orig_sysloghandler.LOG_LOCAL3})]
|
||||||
|
self.assertEqual(
|
||||||
|
expected_args, syslog_handler_args)
|
||||||
|
|
||||||
# Using UDP with default port
|
# Using UDP with default port
|
||||||
syslog_handler_args = []
|
syslog_handler_args = []
|
||||||
@ -1819,6 +1853,15 @@ class TestUtils(unittest.TestCase):
|
|||||||
'facility': orig_sysloghandler.LOG_LOCAL0})],
|
'facility': orig_sysloghandler.LOG_LOCAL0})],
|
||||||
syslog_handler_args)
|
syslog_handler_args)
|
||||||
|
|
||||||
|
with mock.patch.object(utils, 'ThreadSafeSysLogHandler',
|
||||||
|
side_effect=OSError(errno.EPERM, 'oops')):
|
||||||
|
with self.assertRaises(OSError) as cm:
|
||||||
|
utils.get_logger({
|
||||||
|
'log_facility': 'LOG_LOCAL3',
|
||||||
|
'log_address': 'log_address',
|
||||||
|
}, 'server', log_route='server')
|
||||||
|
self.assertEqual(errno.EPERM, cm.exception.errno)
|
||||||
|
|
||||||
@reset_logger_state
|
@reset_logger_state
|
||||||
def test_clean_logger_exception(self):
|
def test_clean_logger_exception(self):
|
||||||
# setup stream logging
|
# setup stream logging
|
||||||
|
Loading…
Reference in New Issue
Block a user