diff --git a/th_cli/test_run/logging.py b/th_cli/test_run/logging.py index 0ad6e64..58c1938 100644 --- a/th_cli/test_run/logging.py +++ b/th_cli/test_run/logging.py @@ -13,6 +13,7 @@ # See the License for the specific language governing permissions and # limitations under the License. # +import datetime import os from loguru import logger @@ -27,13 +28,22 @@ PYTHON_TEST_LEVEL = "PYTHON_TEST" logger.level(PYTHON_TEST_LEVEL, no=22, icon="🐍", color="") +_LOG_TIMESTAMP_FORMAT = "%Y-%m-%d-%H-%M-%S" def configure_logger_for_run(title: str) -> str: # Reset (Remove all sinks from logger) logger.remove() - log_path = os.path.join(config.log_config.output_log_path, f"test_run_{title}.log") - - logger.add(log_path, enqueue=True, format=config.log_config.format) + # Replace colons so the default timestamp-style title does not produce + # filenames that are invalid on Windows (e.g. when logs are shared across + # systems for triage). + safe_title = title.replace(":", "-") + timestamp = datetime.datetime.now().strftime(_LOG_TIMESTAMP_FORMAT) + log_path = os.path.join( + config.log_config.output_log_path, + f"test_run_{safe_title}_{timestamp}.log", + ) + + logger.add(log_path, enqueue=True, format=config.log_config.format, mode="w") return log_path