ClickHouse/tests/integration/helpers/pytest_xdist_logging_to_separate_files.py

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

30 lines
1.2 KiB
Python
Raw Normal View History

2021-07-01 14:41:59 +00:00
import logging
import os.path
2021-07-01 14:41:59 +00:00
# Makes the parallel workers of pytest-xdist to log to separate files.
# Without this function all workers will log to the same log file
# and mix everything together making it much more difficult for troubleshooting.
def setup():
worker_name = os.environ.get("PYTEST_XDIST_WORKER", "master")
if worker_name == "master":
return
logger = logging.getLogger("")
new_handlers = []
handlers_to_remove = []
for handler in logger.handlers:
if isinstance(handler, logging.FileHandler):
filename, ext = os.path.splitext(handler.baseFilename)
if not filename.endswith("-" + worker_name):
new_filename = filename + "-" + worker_name
new_handler = logging.FileHandler(new_filename + ext)
new_handler.setFormatter(handler.formatter)
new_handler.setLevel(handler.level)
new_handlers.append(new_handler)
handlers_to_remove.append(handler)
for new_handler in new_handlers:
logger.addHandler(new_handler)
for handler in handlers_to_remove:
handler.flush()
logger.removeHandler(handler)