Redirect robot.api.logger calls to file as messages are invisible due to XML-RPC

2.1k views Asked by At

In one of my projects we are using Robot Framework with custom keyword libraries in a complex test environment using assorted ECUs and PCs. One keyword library must be controlled by a python remote server via XML-RPC, as it has to be on a different PC.

Now, all important messages from robot.api.logger calls, like logger.debug() oder logger.console() are swallowed due to the XML-RPC. This is a known issue, which also clearly stated in the docs.

For most parts these APIs work exactly like when using with Robot Framework normally. There main limitation is that logging using robot.api.logger or Python's logging module is currently not supported.

It is possible to write a thin wrapper or decorator for robot.api.logger, so that all debug messages are redirected to a simple txt file, like:

DEBUG HH:MM:SS > Message
WARN  HH:MM:SS > Message

This would be really helpful in case of problems.

Of course, it would be easy to use the built python logging module, but I'm looking to find a solution, that changes the least amout of already existing code and I also want that the results are written to the text file additional to normal robot.api.logging to the Robot reports, as we are using the same library in a local and a remote way.

So basically I need to find a way to extend/redirected robot.api.logger calls, by first using the normal python logging module and then using the normal robot.api.logger.

1

There are 1 answers

0
Bence Kaulics On BEST ANSWER

You can patch the write function of the robot.api.logger so it will write to a log file as well. This patching could be triggered by a library argument. This would require you to only modify the constructor of your library.

RemoteLib.py

import sys

from robot.api import logger
from robot.output import librarylogger

from robotremoteserver import RobotRemoteServer
   
def write(msg, level='INFO', html=False):
    librarylogger.write(msg, level, html)
    with open('log.txt', 'a') as f:
        print(f'{level}\tHH:MM:SS > {msg}', file=f)
    

class RemoteLib():
    ROBOT_LIBRARY_SCOPE = 'GLOBAL'
    ROBOT_LIBRARY_VERSION = 0.1
    
    def __init__(self, to_file=True):
        if to_file:
            logger.write = write
        
    def log_something(self, msg):
        logger.info(msg)
        logger.debug(msg)
        logger.warn(msg)
        logger.trace(msg)
        logger.error(msg)
        logger.console(msg)
        
if __name__ == '__main__':
    RobotRemoteServer(RemoteLib(), *sys.argv[1:])

local_run.robot

*** Settings ***
Library    RemoteLib    to_file=False

*** Test Cases ***
Test
    Log Something    something    

remote_run.robot

*** Settings ***
Library       Remote    http://127.0.0.1:8270

*** Test Cases ***
Test
    Log Something    something    

You could use the Python logging module as well in the write patch just as it is used in Robot Framework itself, Redirects the robot.api.logger to python logging if robot is not running.