Python scripting

@Bob_Nisbet I do not think it is possible to temporary halt a Python Script node and look at the input. What you can do is log the messages and also print() to a log file and take a look at that later:

Here is the ‘pure’ code. You would have to adapt the path.

import logging
import sys
import datetime


class StreamToLogger:

    def __init__(self, logger, level=logging.INFO):
        self.logger = logger
        self.level = level

    def write(self, message):
        if message.rstrip() != "":
            self._log_without_recursion(message.rstrip())

    def flush(self):
        pass

    def _log_without_recursion(self, message):
        original_stdout = sys.stdout
        original_stderr = sys.stderr
        sys.stdout = sys.__stdout__
        sys.stderr = sys.__stderr__

        try:
            self.logger.log(self.level, message)
        finally:
            sys.stdout = original_stdout
            sys.stderr = original_stderr


def stop_logging():
    sys.stdout = sys.__stdout__
    sys.stderr = sys.__stderr__


timestamp = datetime.datetime.now().strftime("%Y-%m-%d_%H-%M-%S")

# adapt this path to your needs (obviously)
logfile_path = "/Users/m_lauber/Dropbox/knime-workspace/hub/kn_example_python_log_to_file/data/"

# Create a filename with the timestamp
logfile = f"{logfile_path}logfile_{timestamp}.txt"

logger = logging.getLogger("my_logger")
logger.setLevel(logging.DEBUG)

# Create file handler and add it to logger
fh = logging.FileHandler(logfile, mode='w')
fh.setLevel(logging.DEBUG)
fh.setFormatter(logging.Formatter("%(asctime)s %(name)-12s %(levelname)-8s %(message)s", datefmt="%Y-%m-%d %H:%M:%S"))
logger.addHandler(fh)

# Logging levels provided by Python's logging library are:
# logging.DEBUG: Detailed information, typically of interest only when diagnosing problems.
# logging.INFO: Confirmation that things are working as expected.
# logging.WARNING: An indication that something unexpected happened or indicative of some problem in the near future (e.g., 'disk space low'). The software is still working as expected.
# logging.ERROR: Due to a more serious problem, the software has not been able to perform some function.
# logging.CRITICAL: A very serious error, indicating that the program itself may be unable to continue running.

# Redirect stdout and stderr to logger
sys.stdout = StreamToLogger(logger, logging.DEBUG)
sys.stderr = StreamToLogger(logger, logging.DEBUG)

# ---- you Python code goes here 

logger.debug("This is a debug message.")
logger.info("This is an info message.")
logger.warning("This is a warning message.")
logger.error("This is an error message.")
logger.critical("This is a critical message.")


print("Print this message")

x = 2 / 1

print(x)

# ----- Stop logging to the file
stop_logging()

2 Likes