alex-astronomer commented on a change in pull request #21281:
URL: https://github.com/apache/airflow/pull/21281#discussion_r806075120



##########
File path: airflow/utils/log/secrets_masker.py
##########
@@ -252,3 +253,44 @@ def add_mask(self, secret: Union[str, dict, Iterable], 
name: Optional[str] = Non
         elif isinstance(secret, collections.abc.Iterable):
             for v in secret:
                 self.add_mask(v, name)
+
+
+class StdoutRedactContext:
+    """
+    This class redirects all stdout within the context through the logger 
specified in the init.
+    Print statements get turned into log lines through the logger member.
+
+    """
+
+    def __init__(self):
+        self.stdout_lines = []
+        self._redirector = contextlib.redirect_stdout(self)
+
+    def write(self, stdout_line):
+        """Write stdout to the internal buffer when stdout is written to."""
+        self.stdout_lines.append(redact(stdout_line))
+
+    def flush(self):
+        """
+        No flush functionality needed here, but this method is needed to make 
this class a "file-like" which
+        can be called by the context lib in self._redirector.
+
+        """
+
+    def __enter__(self):
+        """
+        At the beginning of the context, enter the "redirect mode" which is 
initiated by entering the context
+        of `contextlib.redirect_stdout`.
+
+        """
+        self._redirector.__enter__()
+
+    def __exit__(self, *args):
+        """
+        On exit from the context, close the `contextlib.redirect_stdout` 
context to stop capturing lines
+        from stdout, then route all lines one by one through self.redirect_to.
+
+        """
+        self._redirector.__exit__(*args)
+        for log in self.stdout_lines:
+            sys.stdout.write(log)

Review comment:
       So just to make sure that I'm understanding all of this right, we're 
basically saving a copy of sys.stdout from before it gets redirected, and then 
writing to that which bypasses the redirect context that we've already defined 
before that write occurs?  And the reason that we use the dataclass decorator 
is just to simplify the initialization of self.stdout and save a few lines 
since we don't need to use `__init__`?




-- 
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.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to