chiark / gitweb /
python: add systemd.id128 module
[elogind.git] / src / python-systemd / journal.py
index 760d2db0145670704226a110a61fc1ca760412cf..fc57437c28ac60716ab63ff4d3e59d9c97b6b46d 100644 (file)
@@ -1,8 +1,10 @@
-#  -*- Mode: python; indent-tabs-mode: nil -*- */
+#  -*- Mode: python; coding:utf-8; indent-tabs-mode: nil -*- */
 #
 #  This file is part of systemd.
 #
-#  Copyright 2012 David Strauss
+#  Copyright 2012 David Strauss <david@davidstrauss.net>
+#  Copyright 2012 Zbigniew JÄ™drzejewski-Szmek <zbyszek@in.waw.pl>
+#  Copyright 2012 Marti Raudsepp <marti@juffo.org>
 #
 #  systemd is free software; you can redistribute it and/or modify it
 #  under the terms of the GNU Lesser General Public License as published by
@@ -19,6 +21,7 @@
 
 import traceback as _traceback
 import os as _os
+import logging as _logging
 from syslog import (LOG_EMERG, LOG_ALERT, LOG_CRIT, LOG_ERR,
                     LOG_WARNING, LOG_NOTICE, LOG_INFO, LOG_DEBUG)
 from ._journal import sendv, stream_fd
@@ -114,3 +117,87 @@ def stream(identifier, priority=LOG_DEBUG, level_prefix=False):
 
         fd = stream_fd(identifier, priority, level_prefix)
         return _os.fdopen(fd, 'w', 1)
+
+class JournalHandler(_logging.Handler):
+        """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 or
+        fileConfig, specify 'systemd.journal.JournalHandler' as the
+        handler class.  Only standard handler configuration options
+        are supported: level, formatter, filters.
+
+        The following journal fields will be sent:
+
+        MESSAGE, PRIORITY, THREAD_NAME, CODE_FILE, CODE_LINE,
+        CODE_FUNC, LOGGER (name as supplied to getLogger call),
+        MESSAGE_ID (optional, see above).
+        """
+
+        def emit(self, record):
+                """Write record as journal event.
+
+                MESSAGE is taken from the message provided by the
+                user, and PRIORITY, LOGGER, THREAD_NAME,
+                CODE_{FILE,LINE,FUNC} fields are appended
+                automatically. In addition, record.MESSAGE_ID will be
+                used if present.
+                """
+                try:
+                        msg = self.format(record)
+                        pri = self.mapPriority(record.levelno)
+                        mid = getattr(record, 'MESSAGE_ID', None)
+                        send(msg,
+                             MESSAGE_ID=mid,
+                             PRIORITY=format(pri),
+                             LOGGER=record.name,
+                             THREAD_NAME=record.threadName,
+                             CODE_FILE=record.pathname,
+                             CODE_LINE=record.lineno,
+                             CODE_FUNC=record.funcName)
+                except Exception:
+                        self.handleError(record)
+
+        @staticmethod
+        def mapPriority(levelno):
+                """Map logging levels to journald priorities.
+
+                Since Python log level numbers are "sparse", we have
+                to map numbers in between the standard levels too.
+                """
+                if levelno <= _logging.DEBUG:
+                        return LOG_DEBUG
+                elif levelno <= _logging.INFO:
+                        return LOG_INFO
+                elif levelno <= _logging.WARNING:
+                        return LOG_WARNING
+                elif levelno <= _logging.ERROR:
+                        return LOG_ERR
+                elif levelno <= _logging.CRITICAL:
+                        return LOG_CRIT
+                else:
+                        return LOG_ALERT