Remove journald support
This commit is contained in:
parent
6b46b01fcb
commit
d8f1565f5b
@ -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:
|
Log messages are sent to the first available target of:
|
||||||
|
|
||||||
- Error stream specified by the WSGI server in wsgi.errors
|
- Error stream specified by the WSGI server in "wsgi.errors"
|
||||||
- systemd-journald
|
- ``sys.stderr``
|
||||||
- stderr
|
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import contextlib
|
import contextlib
|
||||||
import io
|
|
||||||
import logging
|
import logging
|
||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
import threading
|
import threading
|
||||||
|
|
||||||
try:
|
|
||||||
import systemd.journal
|
|
||||||
except ImportError:
|
|
||||||
systemd = None
|
|
||||||
|
|
||||||
LOGGER_NAME = "radicale"
|
LOGGER_NAME = "radicale"
|
||||||
LOGGER_FORMAT = "[%(asctime)s] [%(ident)s] [%(levelname)s] %(message)s"
|
LOGGER_FORMAT = "[%(asctime)s] [%(ident)s] [%(levelname)s] %(message)s"
|
||||||
DATE_FORMAT = "%Y-%m-%d %H:%M:%S %z"
|
DATE_FORMAT = "%Y-%m-%d %H:%M:%S %z"
|
||||||
@ -71,26 +64,19 @@ class IdentLogRecordFactory:
|
|||||||
return record
|
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"
|
terminator = "\n"
|
||||||
|
|
||||||
def __init__(self, fallback_stream, fallback_handler):
|
def __init__(self):
|
||||||
super().__init__()
|
super().__init__()
|
||||||
self._streams = {}
|
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):
|
def emit(self, record):
|
||||||
try:
|
try:
|
||||||
stream = self._streams.get(threading.get_ident())
|
stream = self._streams.get(threading.get_ident(), sys.stderr)
|
||||||
if stream is None:
|
|
||||||
self.fallback_handler.emit(record)
|
|
||||||
else:
|
|
||||||
msg = self.format(record)
|
msg = self.format(record)
|
||||||
stream.write(msg)
|
stream.write(msg)
|
||||||
stream.write(self.terminator)
|
stream.write(self.terminator)
|
||||||
@ -101,9 +87,7 @@ class ThreadStreamsHandler(logging.Handler):
|
|||||||
|
|
||||||
@contextlib.contextmanager
|
@contextlib.contextmanager
|
||||||
def register_stream(self, stream):
|
def register_stream(self, stream):
|
||||||
if stream == self.fallback_stream:
|
"""Register stream for logging output of the current thread."""
|
||||||
yield
|
|
||||||
return
|
|
||||||
key = threading.get_ident()
|
key = threading.get_ident()
|
||||||
self._streams[key] = stream
|
self._streams[key] = stream
|
||||||
try:
|
try:
|
||||||
@ -112,30 +96,16 @@ class ThreadStreamsHandler(logging.Handler):
|
|||||||
del self._streams[key]
|
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
|
@contextlib.contextmanager
|
||||||
def register_stream(stream):
|
def register_stream(stream):
|
||||||
"""Register global errors stream for the current thread."""
|
"""Register stream for logging output of the current thread."""
|
||||||
yield
|
yield
|
||||||
|
|
||||||
|
|
||||||
def setup():
|
def setup():
|
||||||
"""Set global logging up."""
|
"""Set global logging up."""
|
||||||
global register_stream
|
global register_stream
|
||||||
handler = ThreadStreamsHandler(sys.stderr, get_default_handler())
|
handler = ThreadedStreamHandler()
|
||||||
logging.basicConfig(format=LOGGER_FORMAT, datefmt=DATE_FORMAT,
|
logging.basicConfig(format=LOGGER_FORMAT, datefmt=DATE_FORMAT,
|
||||||
handlers=[handler])
|
handlers=[handler])
|
||||||
register_stream = handler.register_stream
|
register_stream = handler.register_stream
|
||||||
|
Loading…
Reference in New Issue
Block a user