Detect systemd journal

This commit is contained in:
Unrud 2018-08-16 07:59:57 +02:00
parent 24815255be
commit bd52dcd590

View File

@ -23,10 +23,16 @@ http://docs.python.org/library/logging.config.html
""" """
import contextlib import contextlib
import io
import logging import logging
import os
import sys import sys
import threading import threading
try:
from systemd import journal
except ImportError:
journal = None
LOGGER_NAME = "radicale" LOGGER_NAME = "radicale"
LOGGER_FORMAT = "[%(processName)s/%(threadName)s] %(levelname)s: %(message)s" LOGGER_FORMAT = "[%(processName)s/%(threadName)s] %(levelname)s: %(message)s"
@ -87,6 +93,14 @@ class ThreadStreamsHandler(logging.Handler):
def get_default_handler(): def get_default_handler():
handler = logging.StreamHandler(sys.stderr) handler = logging.StreamHandler(sys.stderr)
# Detect systemd journal
with contextlib.suppress(ValueError, io.UnsupportedOperation):
journal_dev, journal_ino = map(
int, os.environ.get("JOURNAL_STREAM", "").split(":"))
st = os.fstat(sys.stderr.fileno())
if (journal and
st.st_dev == journal_dev and st.st_ino == journal_ino):
handler = journal.JournalHandler(SYSLOG_IDENTIFIER=LOGGER_NAME)
return handler return handler