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 2 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 exceptions in tests to stderr.
17 changes: 12 additions & 5 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,7 +36,8 @@ 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. Exceptions are additionally
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not everything logged at logger.error is an exception! s/exceptions/errors/.

Likewise in the changelog, and the name of the handler.

logged to stderr.
"""
root_logger = logging.getLogger()

Expand All @@ -44,11 +46,16 @@ def setup_logging():
"%(levelname)s - %(request)s - %(message)s"
)

handler = ToTwistedHandler()
to_twisted_handler = ToTwistedHandler()
formatter = logging.Formatter(log_format)
handler.setFormatter(formatter)
handler.addFilter(LoggingContextFilter())
root_logger.addHandler(handler)
to_twisted_handler.setFormatter(formatter)
to_twisted_handler.addFilter(LoggingContextFilter())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is redundant, as of #8051. (If it's not redundant, we need to do it for the other handler too.)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alright. I propose to test if this is redundant by

  • removing filter here
  • changing LoggingContextFilter to have some obvious side effect
  • trigger the new log-to-stderr behaviour, and look for the side effect

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Appears to be needed. Otherwise the call to debug in synapse/metrics/init.py:596 will fail because the record doesn't have a "request" field.

root_logger.addHandler(to_twisted_handler)

exception_handler = logging.StreamHandler(sys.stderr)
exception_handler.setLevel(logging.ERROR)
exception_handler.setFormatter(formatter)
root_logger.addHandler(exception_handler)

log_level = os.environ.get("SYNAPSE_TEST_LOG_LEVEL", "ERROR")
root_logger.setLevel(log_level)