log.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. # This file is part of Radicale Server - Calendar 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 contextlib
  24. import logging
  25. import os
  26. import sys
  27. import threading
  28. LOGGER_NAME = "radicale"
  29. LOGGER_FORMAT = "[%(asctime)s] [%(ident)s] [%(levelname)s] %(message)s"
  30. DATE_FORMAT = "%Y-%m-%d %H:%M:%S %z"
  31. logger = logging.getLogger(LOGGER_NAME)
  32. class RemoveTracebackFilter(logging.Filter):
  33. def filter(self, record):
  34. record.exc_info = None
  35. return True
  36. REMOVE_TRACEBACK_FILTER = RemoveTracebackFilter()
  37. class IdentLogRecordFactory:
  38. """LogRecordFactory that adds ``ident`` attribute."""
  39. def __init__(self, upstream_factory):
  40. self.upstream_factory = upstream_factory
  41. def __call__(self, *args, **kwargs):
  42. record = self.upstream_factory(*args, **kwargs)
  43. ident = "%d" % os.getpid()
  44. main_thread = threading.main_thread()
  45. current_thread = threading.current_thread()
  46. if current_thread.name and main_thread != current_thread:
  47. ident += "/%s" % current_thread.name
  48. record.ident = ident
  49. return record
  50. class ThreadedStreamHandler(logging.Handler):
  51. """Sends logging output to the stream registered for the current thread or
  52. ``sys.stderr`` when no stream was registered."""
  53. terminator = "\n"
  54. def __init__(self):
  55. super().__init__()
  56. self._streams = {}
  57. def emit(self, record):
  58. try:
  59. stream = self._streams.get(threading.get_ident(), sys.stderr)
  60. msg = self.format(record)
  61. stream.write(msg)
  62. stream.write(self.terminator)
  63. if hasattr(stream, "flush"):
  64. stream.flush()
  65. except Exception:
  66. self.handleError(record)
  67. @contextlib.contextmanager
  68. def register_stream(self, stream):
  69. """Register stream for logging output of the current thread."""
  70. key = threading.get_ident()
  71. self._streams[key] = stream
  72. try:
  73. yield
  74. finally:
  75. del self._streams[key]
  76. @contextlib.contextmanager
  77. def register_stream(stream):
  78. """Register stream for logging output of the current thread."""
  79. yield
  80. def setup():
  81. """Set global logging up."""
  82. global register_stream
  83. handler = ThreadedStreamHandler()
  84. logging.basicConfig(format=LOGGER_FORMAT, datefmt=DATE_FORMAT,
  85. handlers=[handler])
  86. register_stream = handler.register_stream
  87. log_record_factory = IdentLogRecordFactory(logging.getLogRecordFactory())
  88. logging.setLogRecordFactory(log_record_factory)
  89. set_level(logging.WARNING)
  90. def set_level(level):
  91. """Set logging level for global logger."""
  92. if isinstance(level, str):
  93. level = getattr(logging, level.upper())
  94. logger.setLevel(level)
  95. if level == logging.DEBUG:
  96. logger.removeFilter(REMOVE_TRACEBACK_FILTER)
  97. else:
  98. logger.addFilter(REMOVE_TRACEBACK_FILTER)