You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pulsar.apache.org by je...@apache.org on 2019/01/03 06:27:27 UTC

[pulsar] branch master updated: output from 'print' statements should be handled correctly in python functions (#3286)

This is an automated email from the ASF dual-hosted git repository.

jerrypeng pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/pulsar.git


The following commit(s) were added to refs/heads/master by this push:
     new 223c0c4  output from 'print' statements should be handled correctly in python functions (#3286)
223c0c4 is described below

commit 223c0c4a428e6fa4d56df94a96f2d9267d7819f8
Author: Boyang Jerry Peng <je...@gmail.com>
AuthorDate: Wed Jan 2 22:27:22 2019 -0800

    output from 'print' statements should be handled correctly in python functions (#3286)
---
 pulsar-functions/instance/src/main/python/log.py | 24 ++++++++++++++++++++++++
 1 file changed, 24 insertions(+)

diff --git a/pulsar-functions/instance/src/main/python/log.py b/pulsar-functions/instance/src/main/python/log.py
index f8ee10c..a5ba15f 100644
--- a/pulsar-functions/instance/src/main/python/log.py
+++ b/pulsar-functions/instance/src/main/python/log.py
@@ -27,6 +27,7 @@ import logging.handlers
 import os
 import errno
 import pulsar
+import sys
 
 # Create the logger
 # pylint: disable=invalid-name
@@ -92,3 +93,26 @@ def init_logger(level, logfile, logging_config_file):
   Log = logging.getLogger()
   Log.setLevel(level)
 
+  # set print to redirect to logger
+  class StreamToLogger(object):
+    """
+    Fake file-like stream object that redirects writes to a logger instance.
+    """
+
+    def __init__(self, logger, log_level=logging.INFO):
+      self.logger = logger
+      self.log_level = log_level
+      self.linebuf = ''
+
+    def write(self, buf):
+      for line in buf.rstrip().splitlines():
+        self.logger.log(self.log_level, line.rstrip())
+
+    def flush(self):
+      pass
+
+  sl = StreamToLogger(Log, logging.INFO)
+  sys.stdout = sl
+
+  sl = StreamToLogger(Log, logging.ERROR)
+  sys.stderr = sl