dianfu commented on a change in pull request #15780:
URL: https://github.com/apache/flink/pull/15780#discussion_r632316870



##########
File path: flink-python/pyflink/fn_execution/beam/beam_sdk_worker_main.py
##########
@@ -25,5 +25,44 @@
 
 import apache_beam.runners.worker.sdk_worker_main
 
+
+def custom_logging(logging_func, msg, *args, **kwargs):

Review comment:
       ```suggestion
   def print_to_logging(logging_func, msg, *args, **kwargs):
   ```

##########
File path: flink-python/pyflink/fn_execution/beam/beam_sdk_worker_main.py
##########
@@ -25,5 +25,44 @@
 
 import apache_beam.runners.worker.sdk_worker_main
 
+
+def custom_logging(logging_func, msg, *args, **kwargs):
+    if msg != '\n':
+        logging_func(msg, *args, **kwargs)
+
+
+class CustomPrint(object):
+    def __init__(self, _print):
+        self._msg_buffer = []
+        self._print = _print
+
+    def print(self, *args, sep=' ', end='\n', file=None):
+        self._msg_buffer.append(sep.join([str(arg) for arg in args]))
+        if end == '\n':
+            self._print(''.join(self._msg_buffer), sep=sep, end=end, file=file)
+            self._msg_buffer.clear()
+        else:
+            self._msg_buffer.append(end)
+
+    def close(self):
+        if self._msg_buffer:
+            self._print(''.join(self._msg_buffer), sep='', end='\n')
+            self._msg_buffer.clear()
+
+
 if __name__ == '__main__':
+    import builtins
+    import logging
+    from functools import partial
+
+    # redirect stdout to logging.info, stderr to logging.error
+    _info = logging.getLogger().info
+    _error = logging.getLogger().error
+    sys.stdout.write = partial(custom_logging, _info)
+    sys.stderr.write = partial(custom_logging, _error)
+
+    _print = print
+    custom_print = CustomPrint(_print)

Review comment:
       ```suggestion
       custom_print = CustomPrint(_print=print)
   ```
   Then `_print = print` could be removed

##########
File path: docs/content.zh/docs/dev/python/debugging.md
##########
@@ -28,13 +28,16 @@ under the License.
 
 ## 打印日志信息
 
-Python UDF可以通过标准的Python logging模块记录上下文和调试信息。
+Python UDF可以通过 `print` 或者标准的Python logging模块记录上下文和调试信息。

Review comment:
       ```suggestion
   Python UDF 可以通过 `print` 或者标准的 Python logging 模块记录上下文和调试信息。
   ```




-- 
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:
[email protected]


Reply via email to