From 19fd9a2e2a283705552cb2c1f53c229b6ecf85e1 Mon Sep 17 00:00:00 2001 From: Benjamin Reichel Date: Thu, 9 Apr 2020 15:02:20 +0200 Subject: [PATCH] Add missing SYSLOG_FACILITY to JournalHandler Without SYSLOG_FACILITY log messages from journald forwards the message to kern.log. This seems to be default behaviour as it treats the message as kernel message. This option provides a proper facility to the log message and kern.log will not be filled up anymore. Cherry-picked from commit 5b12e66f6283962d96884e4aa3fa7db7d4e062fa Cherry-picked from commit 82751ce2a2b913a5d107133a15960c9c6d8fd594 Cherry-picked from commit 911a45401c1292b1946c200aa0f6a98b9e246fd8 Change-Id: I62a32ed46a400b62ead8c1e6e64acee658e2a769 Closes-Bug: #1871840 --- oslo_log/handlers.py | 16 ++++++++++++---- oslo_log/log.py | 6 ++++-- oslo_log/tests/unit/test_log.py | 8 ++++++-- ...add-facility-to-journal-e10bf7002cc19dd3.yaml | 7 +++++++ 4 files changed, 29 insertions(+), 8 deletions(-) create mode 100644 releasenotes/notes/add-facility-to-journal-e10bf7002cc19dd3.yaml diff --git a/oslo_log/handlers.py b/oslo_log/handlers.py index d6028429..d4f5fc79 100644 --- a/oslo_log/handlers.py +++ b/oslo_log/handlers.py @@ -107,13 +107,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) @@ -128,7 +135,8 @@ class OSJournalHandler(logging.Handler): '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: diff --git a/oslo_log/log.py b/oslo_log/log.py index 8cf5d496..6d0dc3a1 100644 --- a/oslo_log/log.py +++ b/oslo_log/log.py @@ -388,7 +388,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: @@ -413,7 +416,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) diff --git a/oslo_log/tests/unit/test_log.py b/oslo_log/tests/unit/test_log.py index 76f614a3..9ff58cf6 100644 --- a/oslo_log/tests/unit/test_log.py +++ b/oslo_log/tests/unit/test_log.py @@ -403,6 +403,7 @@ class OSJournalHandlerTestCase(BaseTestCase): 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', @@ -415,7 +416,8 @@ class OSJournalHandlerTestCase(BaseTestCase): self.assertIsInstance(args[0], six.string_types) 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, six.string_types) self.assertIsInstance(arg, six.string_types + (six.binary_type,)) @@ -432,6 +434,7 @@ class OSJournalHandlerTestCase(BaseTestCase): 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, @@ -446,7 +449,8 @@ class OSJournalHandlerTestCase(BaseTestCase): self.assertIsInstance(args[0], six.string_types) 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, six.string_types) self.assertIsInstance(arg, six.string_types + (six.binary_type,)) diff --git a/releasenotes/notes/add-facility-to-journal-e10bf7002cc19dd3.yaml b/releasenotes/notes/add-facility-to-journal-e10bf7002cc19dd3.yaml new file mode 100644 index 00000000..09fdd686 --- /dev/null +++ b/releasenotes/notes/add-facility-to-journal-e10bf7002cc19dd3.yaml @@ -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)