[systemd-devel] PATCH: Integration with Python logging framework

Marti Raudsepp marti at juffo.org
Tue Oct 9 09:36:10 PDT 2012


Hi list,

The attached patch implements journal integration for the Python
logging framework. Tested on my Arch Linux machine, using Python 2.7
and 3.2, but should be compatible with all versions from 2.5 through
3.3

The logging framework has a mechanism for supplying additional
key-value pairs, but I'm currently only passing through MESSAGE_ID.
Are there any other common custom fields that apps are supposed to
use?

Unfortunately it's impossible to tell apart extra fields from the
standard ones, and dumping all fields into journal seems like a bad
idea since they're mostly redundant. The values may also be Python
objects and not strings. Thoughts?

Also, exception tracebacks are logged as one multi-line log message.
Is this a good idea? I see that journalctl has recently gained support
for displaying multiline messages.

Description from docstring:
----
Journal handler class for the Python logging framework.

Please see the Python logging module documentation for an
overview: http://docs.python.org/library/logging.html

To create a custom logger whose messages go only to journal:

>>> log = logging.getLogger('custom_logger_name')
>>> log.propagate = False
>>> log.addHandler(journal.JournalHandler())
>>> log.warn("Some message: %s", detail)

Note that by default, message levels INFO and DEBUG are ignored
by the logging framework. To enable those log levels:

>>> log.setLevel(logging.DEBUG)

To attach journal MESSAGE_ID, an extra field is supported:

>>> log.warn("Message with ID",
>>>     extra={'MESSAGE_ID': '22bb01335f724c959ac4799627d1cb61'})

To redirect all logging messages to journal regardless of where
they come from, attach it to the root logger:

>>> logging.root.addHandler(journal.JournalHandler())

For more complex configurations when using dictConfig/fileConfig,
specify 'systemd.journal.JournalHandler' as the handler class.
Only standard handler configuration options are supported:
level, formatter, filters.

The following journal fields are supported: MESSAGE, PRIORITY,
LOGGER (name as supplied to getLogger call), THREAD_NAME,
CODE_FILE, CODE_LINE, CODE_FUNC, MESSAGE_ID (optional, see above)
----

This is what an example message looks like:
MESSAGE=Message with ID
PRIORITY=4
LOGGER=custom_logger_name
THREAD_NAME=MainThread
CODE_FILE=test.py
CODE_LINE=20
CODE_FUNC=test_journal
MESSAGE_ID=e38633ad25104c50a3f6a43ebe862368

Also available from my GitHub fork:
https://github.com/intgr/systemd/ branch py-logging

----
Regards,
Marti
-------------- next part --------------
A non-text attachment was scrubbed...
Name: 0001-python-integration-with-Python-logging-framework.patch
Type: application/octet-stream
Size: 4295 bytes
Desc: not available
URL: <http://lists.freedesktop.org/archives/systemd-devel/attachments/20121009/82b8e261/attachment.obj>


More information about the systemd-devel mailing list