You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

58 lines
1.8 KiB

  1. # Copyright 2019 New Vector Ltd
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. import logging
  15. import os
  16. import twisted.logger
  17. from synapse.logging.context import LoggingContextFilter
  18. from synapse.synapse_rust import reset_logging_config
  19. class ToTwistedHandler(logging.Handler):
  20. """logging handler which sends the logs to the twisted log"""
  21. tx_log = twisted.logger.Logger()
  22. def emit(self, record: logging.LogRecord) -> None:
  23. log_entry = self.format(record)
  24. log_level = record.levelname.lower().replace("warning", "warn")
  25. self.tx_log.emit(
  26. twisted.logger.LogLevel.levelWithName(log_level), "{entry}", entry=log_entry
  27. )
  28. def setup_logging() -> None:
  29. """Configure the python logging appropriately for the tests.
  30. (Logs will end up in _trial_temp.)
  31. """
  32. root_logger = logging.getLogger()
  33. log_format = (
  34. "%(asctime)s - %(name)s - %(lineno)d - "
  35. "%(levelname)s - %(request)s - %(message)s"
  36. )
  37. handler = ToTwistedHandler()
  38. formatter = logging.Formatter(log_format)
  39. handler.setFormatter(formatter)
  40. handler.addFilter(LoggingContextFilter())
  41. root_logger.addHandler(handler)
  42. log_level = os.environ.get("SYNAPSE_TEST_LOG_LEVEL", "ERROR")
  43. root_logger.setLevel(log_level)
  44. reset_logging_config()