From 5c490386eeaf39807e42e4da98bbd2503b1ceca4 Mon Sep 17 00:00:00 2001 From: Dobiko Date: Tue, 3 Oct 2023 10:52:38 +0200 Subject: [PATCH] Redirect logs based on env var (#1474) (cherry picked from commit 879874491370e3e824fe1e14fe28c06dba2bc6fd) --- src/super_gradients/common/auto_logging/auto_logger.py | 2 +- src/super_gradients/common/auto_logging/console_logging.py | 3 ++- src/super_gradients/common/environment/env_variables.py | 5 +++++ 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/super_gradients/common/auto_logging/auto_logger.py b/src/super_gradients/common/auto_logging/auto_logger.py index 312bf655c1..e0ef358a05 100755 --- a/src/super_gradients/common/auto_logging/auto_logger.py +++ b/src/super_gradients/common/auto_logging/auto_logger.py @@ -32,7 +32,7 @@ def _setup_default_logging(self, log_level: str = None) -> None: # Therefore the log file will have the parent PID to being able to discriminate the logs corresponding to a single run. timestamp = time.strftime("%Y_%m_%d_%H_%M_%S", time.localtime()) self._setup_logging( - filename=os.path.expanduser(f"~/sg_logs/logs_{os.getppid()}_{timestamp}.log"), + filename=os.path.join(env_variables.SUPER_GRADIENTS_LOG_DIR, f"logs_{os.getppid()}_{timestamp}.log"), copy_already_logged_messages=False, filemode="w", log_level=log_level, diff --git a/src/super_gradients/common/auto_logging/console_logging.py b/src/super_gradients/common/auto_logging/console_logging.py index 7a3f8ecbb2..b7a9e0d9aa 100644 --- a/src/super_gradients/common/auto_logging/console_logging.py +++ b/src/super_gradients/common/auto_logging/console_logging.py @@ -7,6 +7,7 @@ from threading import Lock from super_gradients.common.environment.ddp_utils import multi_process_safe, is_main_process +from super_gradients.common.environment.env_variables import env_variables class BufferWriter: @@ -117,7 +118,7 @@ def __init__(self): @multi_process_safe def _setup(self): """On instantiation, setup the default sink file.""" - filename = Path.home() / "sg_logs" / "console.log" + filename = Path(env_variables.SUPER_GRADIENTS_LOG_DIR) / "console.log" filename.parent.mkdir(exist_ok=True) self.filename = str(filename) os.makedirs(os.path.dirname(self.filename), exist_ok=True) diff --git a/src/super_gradients/common/environment/env_variables.py b/src/super_gradients/common/environment/env_variables.py index 24f35933a1..557caca58a 100644 --- a/src/super_gradients/common/environment/env_variables.py +++ b/src/super_gradients/common/environment/env_variables.py @@ -1,4 +1,5 @@ import os +from pathlib import Path from typing import Optional @@ -45,5 +46,9 @@ def HYDRA_FULL_ERROR(self) -> Optional[str]: def HYDRA_FULL_ERROR(self, value: str): os.environ["HYDRA_FULL_ERROR"] = value + @property + def SUPER_GRADIENTS_LOG_DIR(self) -> str: + return os.getenv("SUPER_GRADIENTS_LOG_DIR", default=str(Path.home() / "sg_logs")) + env_variables = EnvironmentVariables()