diff --git a/doc/source/journal.rst b/doc/source/journal.rst index 997275ae..7a0b8431 100644 --- a/doc/source/journal.rst +++ b/doc/source/journal.rst @@ -58,8 +58,17 @@ known at the time of logging the message. CODE_FILE=, CODE_LINE=, CODE_FUNC= The code location generating this message, if known. Contains the - source filename, the line number and the function name. (This is - the same as systemd uses) + source filename, the line number and the function name. (This is the + same as systemd uses) + +THREAD_NAME=, PROCESS_NAME= + + Information about the thread and process, if known. (This is the same + as systemd uses) + +EXCEPTION_TEXT=, EXCEPTION_INFO= + + Information about an exception, if an exception has been logged. LOGGER_NAME= diff --git a/oslo_log/handlers.py b/oslo_log/handlers.py index 8e561a48..74816189 100644 --- a/oslo_log/handlers.py +++ b/oslo_log/handlers.py @@ -123,12 +123,20 @@ class OSJournalHandler(logging.Handler): 'CODE_FILE': record.pathname, 'CODE_LINE': record.lineno, 'CODE_FUNC': record.funcName, + 'THREAD_NAME': record.threadName, + 'PROCESS_NAME': record.processName, 'LOGGER_NAME': record.name, 'LOGGER_LEVEL': record.levelname, 'SYSLOG_IDENTIFIER': self.binary_name, 'PRIORITY': priority } + if record.exc_text: + extras['EXCEPTION_TEXT'] = record.exc_text + + if record.exc_info: + extras['EXCEPTION_INFO'] = record.exc_info + for field in self.custom_fields: value = record.__dict__.get(field) if value: diff --git a/oslo_log/tests/unit/test_log.py b/oslo_log/tests/unit/test_log.py index c68838ef..4d8f2d05 100644 --- a/oslo_log/tests/unit/test_log.py +++ b/oslo_log/tests/unit/test_log.py @@ -334,6 +334,30 @@ class OSJournalHandlerTestCase(BaseTestCase): SYSLOG_IDENTIFIER=mock.ANY, REQUEST_ID=mock.ANY, PROJECT_NAME='mytenant', + PROCESS_NAME='MainProcess', + THREAD_NAME='MainThread', + USER_NAME='myuser')) + + def test_emit_exception(self): + l = log.getLogger('nova-exception.foo') + local_context = _fake_new_context() + try: + raise Exception("Some exception") + except Exception: + l.exception("Foo", context=local_context) + self.assertEqual( + self.journal.send.call_args, + mock.call(mock.ANY, CODE_FILE=mock.ANY, + CODE_FUNC='test_emit_exception', + CODE_LINE=mock.ANY, LOGGER_LEVEL='ERROR', + LOGGER_NAME='nova-exception.foo', PRIORITY=3, + SYSLOG_IDENTIFIER=mock.ANY, + REQUEST_ID=mock.ANY, + EXCEPTION_INFO=mock.ANY, + EXCEPTION_TEXT=mock.ANY, + PROJECT_NAME='mytenant', + PROCESS_NAME='MainProcess', + THREAD_NAME='MainThread', USER_NAME='myuser'))