Skip to content

Commit

Permalink
Merge "Add missing SYSLOG_FACILITY to JournalHandler"
Browse files Browse the repository at this point in the history
  • Loading branch information
Zuul authored and openstack-gerrit committed May 28, 2020
2 parents 3bf223d + 5b12e66 commit d0be4dc
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 8 deletions.
16 changes: 12 additions & 4 deletions oslo_log/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,20 @@ class OSJournalHandler(logging.Handler):
'request_id',
)

def __init__(self):
# Do not use super() unless type(logging.Handler) is 'type'
# (i.e. >= Python 2.7).
def __init__(self, facility):
if not journal:
raise RuntimeError("Systemd bindings do not exist")

if not facility:
if not syslog:
raise RuntimeError("syslog is not available on this platform")
facility = syslog.LOG_USER

# Do not use super() unless type(logging.Handler) is 'type'
# (i.e. >= Python 2.7).
logging.Handler.__init__(self)
self.binary_name = _get_binary_name()
self.facility = facility

def emit(self, record):
priority = SYSLOG_MAP.get(record.levelname, 7)
Expand All @@ -103,7 +110,8 @@ def emit(self, record):
'LOGGER_NAME': record.name,
'LOGGER_LEVEL': record.levelname,
'SYSLOG_IDENTIFIER': self.binary_name,
'PRIORITY': priority
'PRIORITY': priority,
'SYSLOG_FACILITY': self.facility,
}

if record.exc_info:
Expand Down
6 changes: 4 additions & 2 deletions oslo_log/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,10 @@ def _setup_logging_from_conf(conf, project, version):
log_root.addHandler(streamlog)

if conf.use_journal:
journal = handlers.OSJournalHandler()
if syslog is None:
raise RuntimeError("syslog is not available on this platform")
facility = _find_facility(conf.syslog_log_facility)
journal = handlers.OSJournalHandler(facility=facility)
log_root.addHandler(journal)

if conf.use_eventlog:
Expand All @@ -412,7 +415,6 @@ def _setup_logging_from_conf(conf, project, version):
log_root.addHandler(handler)

if conf.use_syslog:
global syslog
if syslog is None:
raise RuntimeError("syslog is not available on this platform")
facility = _find_facility(conf.syslog_log_facility)
Expand Down
8 changes: 6 additions & 2 deletions oslo_log/tests/unit/test_log.py
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,7 @@ def test_emit(self):
mock.call(mock.ANY, CODE_FILE=mock.ANY, CODE_FUNC='test_emit',
CODE_LINE=mock.ANY, LOGGER_LEVEL='INFO',
LOGGER_NAME='nova-test.foo', PRIORITY=6,
SYSLOG_FACILITY=syslog.LOG_USER,
SYSLOG_IDENTIFIER=mock.ANY,
REQUEST_ID=mock.ANY,
PROJECT_NAME='mytenant',
Expand All @@ -410,7 +411,8 @@ def test_emit(self):
self.assertIsInstance(args[0], str)
self.assertIsInstance(kwargs['CODE_LINE'], int)
self.assertIsInstance(kwargs['PRIORITY'], int)
del kwargs['CODE_LINE'], kwargs['PRIORITY']
self.assertIsInstance(kwargs['SYSLOG_FACILITY'], int)
del kwargs['CODE_LINE'], kwargs['PRIORITY'], kwargs['SYSLOG_FACILITY']
for key, arg in kwargs.items():
self.assertIsInstance(key, str)
self.assertIsInstance(arg, (bytes, str))
Expand All @@ -427,6 +429,7 @@ def test_emit_exception(self):
CODE_FUNC='test_emit_exception',
CODE_LINE=mock.ANY, LOGGER_LEVEL='ERROR',
LOGGER_NAME='nova-exception.foo', PRIORITY=3,
SYSLOG_FACILITY=syslog.LOG_USER,
SYSLOG_IDENTIFIER=mock.ANY,
REQUEST_ID=mock.ANY,
EXCEPTION_INFO=mock.ANY,
Expand All @@ -441,7 +444,8 @@ def test_emit_exception(self):
self.assertIsInstance(args[0], str)
self.assertIsInstance(kwargs['CODE_LINE'], int)
self.assertIsInstance(kwargs['PRIORITY'], int)
del kwargs['CODE_LINE'], kwargs['PRIORITY']
self.assertIsInstance(kwargs['SYSLOG_FACILITY'], int)
del kwargs['CODE_LINE'], kwargs['PRIORITY'], kwargs['SYSLOG_FACILITY']
for key, arg in kwargs.items():
self.assertIsInstance(key, str)
self.assertIsInstance(arg, (bytes, str))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
fixes:
- |
Using systemd journal handler, logs ending up in kern.log because
SYSLOG_FACILITY is missing. This fix uses the same facility configuration
option 'syslog-log-facility' as from syslog handler to provide the
missing value. (Bug #1871840)

0 comments on commit d0be4dc

Please sign in to comment.