From e7b5bec4c6fc90e4cf59ccc550b5485046950cd5 Mon Sep 17 00:00:00 2001 From: Patrick Cudrey Date: Wed, 16 Nov 2022 15:19:53 +0100 Subject: [PATCH] Switch log.py logger to independant logger In case there is any kind of issue when setting up logger, use a specific logger for log calls. This avoids recursion issues when trying to operate "low level" calls on logger. --- python/tank/log.py | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/python/tank/log.py b/python/tank/log.py index 65317cb624..ad76f56fd4 100644 --- a/python/tank/log.py +++ b/python/tank/log.py @@ -225,6 +225,7 @@ def my_method(): from logging.handlers import RotatingFileHandler import os import sys +import tempfile import time import weakref import uuid @@ -304,7 +305,7 @@ def doRollover(self): try: os.rename(self.baseFilename, temp_backup_name) - except: + except OSError: # It failed, so we'll simply append from now on. log.debug( "Cannot rotate log file '%s'. Logging will continue to this file, " @@ -319,7 +320,7 @@ def doRollover(self): # so doRollover can do its work. try: os.rename(temp_backup_name, self.baseFilename) - except: + except OSError: # For some reason we couldn't move the backup in its place. log.debug( "Unexpected issue while rotating log file '%s'. Logging will continue to this file, " @@ -344,7 +345,7 @@ def doRollover(self): # disable rollover and append to the current log. try: RotatingFileHandler.doRollover(self) - except: + except Exception: # Something probably failed trying to rollover the backups, # since the code above proved that in theory the main log file # should be renamable. In any case, we didn't succeed in renaming, @@ -824,7 +825,23 @@ def initialize_base_file_handler_from_path(self, log_file): # the logger for logging messages from this file :) -log = LogManager.get_logger(__name__) +# using python default's logger just in case the LogManager fails and we need to debug it, logging to a tempfile +log = logging.getLogger("fallback") +log.setLevel(logging.INFO) +fallback_log_filepath = os.path.join( + tempfile.gettempdir(), + "{}.fallback.log".format(constants.ROOT_LOGGER_NAME) +) +fh = logging.FileHandler(fallback_log_filepath) +formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') +fh.setFormatter(formatter) +log.addHandler(fh) +log.info("fallback default logger setup") + +if os.environ.get(constants.DEBUG_LOGGING_ENV_VAR, False): + log.setLevel(logging.DEBUG) + log.debug("debug mode on") + # initialize toolkit logging #