log.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. # This file is part of Radicale - CalDAV and CardDAV server
  2. # Copyright © 2011-2017 Guillaume Ayoub
  3. # Copyright © 2017-2019 Unrud <unrud@outlook.com>
  4. #
  5. # This library is free software: you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation, either version 3 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # This library is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with Radicale. If not, see <http://www.gnu.org/licenses/>.
  17. """
  18. Functions to set up Python's logging facility for Radicale's WSGI application.
  19. Log messages are sent to the first available target of:
  20. - Error stream specified by the WSGI server in "wsgi.errors"
  21. - ``sys.stderr``
  22. """
  23. import logging
  24. import os
  25. import sys
  26. import threading
  27. from typing import Any, Callable, ClassVar, Dict, Iterator, Union
  28. from radicale import types
  29. LOGGER_NAME: str = "radicale"
  30. LOGGER_FORMAT: str = "[%(asctime)s] [%(ident)s] [%(levelname)s] %(message)s"
  31. DATE_FORMAT: str = "%Y-%m-%d %H:%M:%S %z"
  32. logger: logging.Logger = logging.getLogger(LOGGER_NAME)
  33. class RemoveTracebackFilter(logging.Filter):
  34. def filter(self, record: logging.LogRecord) -> bool:
  35. record.exc_info = None
  36. return True
  37. REMOVE_TRACEBACK_FILTER: logging.Filter = RemoveTracebackFilter()
  38. class IdentLogRecordFactory:
  39. """LogRecordFactory that adds ``ident`` attribute."""
  40. def __init__(self, upstream_factory: Callable[..., logging.LogRecord]
  41. ) -> None:
  42. self._upstream_factory = upstream_factory
  43. def __call__(self, *args: Any, **kwargs: Any) -> logging.LogRecord:
  44. record = self._upstream_factory(*args, **kwargs)
  45. ident = "%d" % os.getpid()
  46. main_thread = threading.main_thread()
  47. current_thread = threading.current_thread()
  48. if current_thread.name and main_thread != current_thread:
  49. ident += "/%s" % current_thread.name
  50. record.ident = ident # type:ignore[attr-defined]
  51. return record
  52. class ThreadedStreamHandler(logging.Handler):
  53. """Sends logging output to the stream registered for the current thread or
  54. ``sys.stderr`` when no stream was registered."""
  55. terminator: ClassVar[str] = "\n"
  56. _streams: Dict[int, types.ErrorStream]
  57. def __init__(self) -> None:
  58. super().__init__()
  59. self._streams = {}
  60. def emit(self, record: logging.LogRecord) -> None:
  61. try:
  62. stream = self._streams.get(threading.get_ident(), sys.stderr)
  63. msg = self.format(record)
  64. stream.write(msg)
  65. stream.write(self.terminator)
  66. if hasattr(stream, "flush"):
  67. stream.flush()
  68. except Exception:
  69. self.handleError(record)
  70. @types.contextmanager
  71. def register_stream(self, stream: types.ErrorStream) -> Iterator[None]:
  72. """Register stream for logging output of the current thread."""
  73. key = threading.get_ident()
  74. self._streams[key] = stream
  75. try:
  76. yield
  77. finally:
  78. del self._streams[key]
  79. @types.contextmanager
  80. def register_stream(stream: types.ErrorStream) -> Iterator[None]:
  81. """Register stream for logging output of the current thread."""
  82. yield
  83. def setup() -> None:
  84. """Set global logging up."""
  85. global register_stream
  86. handler = ThreadedStreamHandler()
  87. logging.basicConfig(format=LOGGER_FORMAT, datefmt=DATE_FORMAT,
  88. handlers=[handler])
  89. register_stream = handler.register_stream
  90. log_record_factory = IdentLogRecordFactory(logging.getLogRecordFactory())
  91. logging.setLogRecordFactory(log_record_factory)
  92. set_level(logging.WARNING)
  93. def set_level(level: Union[int, str]) -> None:
  94. """Set logging level for global logger."""
  95. if isinstance(level, str):
  96. level = getattr(logging, level.upper())
  97. assert isinstance(level, int)
  98. logger.setLevel(level)
  99. logger.removeFilter(REMOVE_TRACEBACK_FILTER)
  100. if level > logging.DEBUG:
  101. logger.addFilter(REMOVE_TRACEBACK_FILTER)