You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airflow.apache.org by GitBox <gi...@apache.org> on 2020/05/06 22:28:26 UTC

[GitHub] [airflow] kaxil commented on a change in pull request #8746: Remove old airflow logger causing side effects in tests

kaxil commented on a change in pull request #8746:
URL: https://github.com/apache/airflow/pull/8746#discussion_r421128887



##########
File path: tests/test_logging_config.py
##########
@@ -97,6 +93,30 @@
 SETTINGS_DEFAULT_NAME = 'custom_airflow_local_settings'
 
 
+def reset_logging():
+    """Reset Logging"""
+    manager = logging.root.manager
+    manager.disabled = logging.NOTSET
+    for logger in manager.loggerDict.values():  # pylint: disable=too-many-nested-blocks
+        if isinstance(logger, logging.Logger):
+            logger.setLevel(logging.NOTSET)
+            logger.propagate = True
+            logger.disabled = False
+            logger.filters.clear()
+            handlers = logger.handlers.copy()
+            for handler in handlers:
+                # Copied from `logging.shutdown`.
+                try:
+                    handler.acquire()
+                    handler.flush()
+                    handler.close()
+                except (OSError, ValueError):
+                    pass
+                finally:
+                    handler.release()
+                logger.removeHandler(handler)

Review comment:
       We can also just use the below code if we want to only reset logging for 'airflow' Loggers:
   
   ```python
       manager = logging.root.manager
       manager.disabled = logging.NOTSET
       airflow_loggers = [
           logger for logger_name, logger in manager.loggerDict.items() if logger_name.startswith('airflow')
       ]
       for logger in airflow_loggers:  # pylint: disable=too-many-nested-blocks
           if isinstance(logger, logging.Logger):
               logger.setLevel(logging.NOTSET)
               logger.propagate = True
               logger.disabled = False
               logger.filters.clear()
               handlers = logger.handlers.copy()
               for handler in handlers:
                   # Copied from `logging.shutdown`.
                   try:
                       handler.acquire()
                       handler.flush()
                       handler.close()
                   except (OSError, ValueError):
                       pass
                   finally:
                       handler.release()
                   logger.removeHandler(handler)
   ```




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org