jerrypeng closed pull request #3286: output from 'print' statements should be handled correctly in python functions URL: https://github.com/apache/pulsar/pull/3286
This is a PR merged from a forked repository. As GitHub hides the original diff on merge, it is displayed below for the sake of provenance: As this is a foreign pull request (from a fork), the diff is supplied below (as it won't show otherwise due to GitHub magic): diff --git a/pulsar-functions/instance/src/main/python/log.py b/pulsar-functions/instance/src/main/python/log.py index f8ee10c8e5..a5ba15fc7c 100644 --- a/pulsar-functions/instance/src/main/python/log.py +++ b/pulsar-functions/instance/src/main/python/log.py @@ -27,6 +27,7 @@ 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 ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected] With regards, Apache Git Services
