Compare commits

...

3 Commits

Author SHA1 Message Date
David Robertson
0dfd550e38 I'm logging errors too. LoggingContextFilter. 2021-08-19 13:35:38 +01:00
David Robertson
ee0748a42a Changelog 2021-08-19 13:09:18 +01:00
David Robertson
c0233eae5e Log exceptions in tests to stderr
This makes it easy to see errors in background processes when you're
writing unit tests. Otherwise, you have to know/remember to look at the
test logs in `_trial_temp`.
2021-08-19 13:06:52 +01:00
2 changed files with 16 additions and 6 deletions

1
changelog.d/10657.misc Normal file
View File

@@ -0,0 +1 @@
Log errors in tests to stderr.

View File

@@ -13,6 +13,7 @@
# limitations under the License.
import logging
import os
import sys
import twisted.logger
@@ -35,7 +36,8 @@ class ToTwistedHandler(logging.Handler):
def setup_logging():
"""Configure the python logging appropriately for the tests.
(Logs will end up in _trial_temp.)
Logs will end up in _trial_temp. Errors are additionally
logged to stderr.
"""
root_logger = logging.getLogger()
@@ -43,12 +45,19 @@ def setup_logging():
"%(asctime)s - %(name)s - %(lineno)d - "
"%(levelname)s - %(request)s - %(message)s"
)
handler = ToTwistedHandler()
formatter = logging.Formatter(log_format)
handler.setFormatter(formatter)
handler.addFilter(LoggingContextFilter())
root_logger.addHandler(handler)
filter = LoggingContextFilter()
to_twisted_handler = ToTwistedHandler()
to_twisted_handler.setFormatter(formatter)
to_twisted_handler.addFilter(filter)
root_logger.addHandler(to_twisted_handler)
error_handler = logging.StreamHandler(sys.stderr)
error_handler.setLevel(logging.ERROR)
error_handler.setFormatter(formatter)
error_handler.addFilter(filter)
root_logger.addHandler(error_handler)
log_level = os.environ.get("SYNAPSE_TEST_LOG_LEVEL", "ERROR")
root_logger.setLevel(log_level)