Remove journald support

This commit is contained in:
Unrud 2020-02-19 09:50:45 +01:00
parent 6b46b01fcb
commit d8f1565f5b

View File

@ -20,24 +20,17 @@ Functions to set up Python's logging facility for Radicale's WSGI application.
Log messages are sent to the first available target of:
- Error stream specified by the WSGI server in wsgi.errors
- systemd-journald
- stderr
- Error stream specified by the WSGI server in "wsgi.errors"
- ``sys.stderr``
"""
import contextlib
import io
import logging
import os
import sys
import threading
try:
import systemd.journal
except ImportError:
systemd = None
LOGGER_NAME = "radicale"
LOGGER_FORMAT = "[%(asctime)s] [%(ident)s] [%(levelname)s] %(message)s"
DATE_FORMAT = "%Y-%m-%d %H:%M:%S %z"
@ -71,26 +64,19 @@ class IdentLogRecordFactory:
return record
class ThreadStreamsHandler(logging.Handler):
class ThreadedStreamHandler(logging.Handler):
"""Sends logging output to the stream registered for the current thread or
``sys.stderr`` when no stream was registered."""
terminator = "\n"
def __init__(self, fallback_stream, fallback_handler):
def __init__(self):
super().__init__()
self._streams = {}
self.fallback_stream = fallback_stream
self.fallback_handler = fallback_handler
def setFormatter(self, fmt):
super().setFormatter(fmt)
self.fallback_handler.setFormatter(fmt)
def emit(self, record):
try:
stream = self._streams.get(threading.get_ident())
if stream is None:
self.fallback_handler.emit(record)
else:
stream = self._streams.get(threading.get_ident(), sys.stderr)
msg = self.format(record)
stream.write(msg)
stream.write(self.terminator)
@ -101,9 +87,7 @@ class ThreadStreamsHandler(logging.Handler):
@contextlib.contextmanager
def register_stream(self, stream):
if stream == self.fallback_stream:
yield
return
"""Register stream for logging output of the current thread."""
key = threading.get_ident()
self._streams[key] = stream
try:
@ -112,30 +96,16 @@ class ThreadStreamsHandler(logging.Handler):
del self._streams[key]
def get_default_handler():
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 (systemd and
st.st_dev == journal_dev and st.st_ino == journal_ino):
handler = systemd.journal.JournalHandler(
SYSLOG_IDENTIFIER=LOGGER_NAME)
return handler
@contextlib.contextmanager
def register_stream(stream):
"""Register global errors stream for the current thread."""
"""Register stream for logging output of the current thread."""
yield
def setup():
"""Set global logging up."""
global register_stream
handler = ThreadStreamsHandler(sys.stderr, get_default_handler())
handler = ThreadedStreamHandler()
logging.basicConfig(format=LOGGER_FORMAT, datefmt=DATE_FORMAT,
handlers=[handler])
register_stream = handler.register_stream