2021-07-01 14:41:59 +00:00
|
|
|
import logging
|
|
|
|
import os.path
|
|
|
|
|
|
|
|
# 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():
|
2022-03-22 16:39:58 +00:00
|
|
|
worker_name = os.environ.get("PYTEST_XDIST_WORKER", "master")
|
|
|
|
if worker_name == "master":
|
2021-07-01 14:41:59 +00:00
|
|
|
return
|
2022-03-22 16:39:58 +00:00
|
|
|
logger = logging.getLogger("")
|
2021-07-01 14:41:59 +00:00
|
|
|
new_handlers = []
|
|
|
|
handlers_to_remove = []
|
|
|
|
for handler in logger.handlers:
|
|
|
|
if isinstance(handler, logging.FileHandler):
|
|
|
|
filename, ext = os.path.splitext(handler.baseFilename)
|
2022-03-22 16:39:58 +00:00
|
|
|
if not filename.endswith("-" + worker_name):
|
|
|
|
new_filename = filename + "-" + worker_name
|
2021-07-01 14:41:59 +00:00
|
|
|
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)
|