logging from python to robot framework not working for subprocess.call

Viewed 431

I have a function called Log that writes a log string to the robot framework log files. If I call this function from within A.py where its defined , it logs into the output.html as expected. But if i call this function from another python function init.py which in turn is invoked from A.py itself using subprocess.call, It does not log into output.html. It simply ignores it. A.py:

def Log( logString,typeOfLog):
        ts = time.time()
        st = datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S')
        print(st)
        if typeOfLog=="INFO":
                logger.console(st + '  ' + logString)
                logger.info(logString)
        elif typeOfLog=="DEBUG":
                logger.console(st + '  ' + logString)
                logger.debug(logString)
        elif typeOfLog=="WARN":
                logger.console(st + '  ' + logString)
                logger.warn(logString)
        elif typeOfLog=="ERROR":
                logger.console(st + '  ' + logString)
                logger.error(st + '  ' + logString)
        elif typeOfLog=="":
                logger.console(logString)
                logger.trace(logString)
.
.
.
.
def Test():
        Log("My debug statement in DEPLOY.PY","INFO")----->gets logged to output.html
        ret = subprocess.call("python init.py", shell=True)
        .
        .
        .

Code for init.py:

from A import Log
from robot.api import logger
def initfunc1():
        Log("Something","INFO")------>this does not get logged to output.html

initfunc1()

IF someone can explain why this is happening and how to make the calls to Log function from init.py also to log into output.html as expected, it would be of great help to me.

Thanks in advance.

1 Answers

The process started with subprocess.call does not have access to the Robot Frameworks (RF) execution context that would be required for it to be able to write log messages into the RF output files (output.xml, log.html, ...). In this case, as the process is unable to find the context, the robot.api.logger will log the messages with python logging module, see logger docs for details.

In order to get output from the subprocess to the robot logs, you could use subprocess.run instead of subprocess.call to capture the output of the child process and log this captured output to the RF output. See also the RF Process library for another way to execute processes from RF.

Related