Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Log exceptions in tests to stderr #10657

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/10657.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Log errors in tests to stderr.
21 changes: 15 additions & 6 deletions tests/test_utils/logging_setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# limitations under the License.
import logging
import os
import sys

import twisted.logger

Expand All @@ -35,20 +36,28 @@ def emit(self, record):
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()

log_format = (
"%(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)