Skip to content

Commit

Permalink
Add additional info like python-systemd does
Browse files Browse the repository at this point in the history
Upstream python-systemd has a journald logging handler that also adds
some exception information, as well as thread information.

  https://github.com/systemd/python-systemd/blob/master/systemd/journal.py#L581-L589

While OpenStack doesn't use a lot of threads, we might as well support it
since it's no cost. Also, add PROCESS_NAME just to be compatible with
other programs that might use python journald logging. Not entirely sure
if record.processName and the results of our self.binary_name will be
different.

The exception info is the fun stuff though.

Change-Id: Ibf0d7dae7587639737e0327f0338f30cdfc6aba1
  • Loading branch information
emonty committed Apr 19, 2017
1 parent 8593e2e commit a5ee482
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 2 deletions.
13 changes: 11 additions & 2 deletions doc/source/journal.rst
Expand Up @@ -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=

Expand Down
8 changes: 8 additions & 0 deletions oslo_log/handlers.py
Expand Up @@ -123,12 +123,20 @@ def emit(self, record):
'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:
Expand Down
24 changes: 24 additions & 0 deletions oslo_log/tests/unit/test_log.py
Expand Up @@ -334,6 +334,30 @@ def test_emit(self):
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'))


Expand Down

0 comments on commit a5ee482

Please sign in to comment.