简体   繁体   中英

Console logging from Robot Framework listeners using Python's logging

Current use case: Say I've a standalone Python library called X.

  • I call X using my created Robot library that uses Robot listeners.
  • The X library I call uses Python's logging module to do logging.
  • X library shouldn't itself be modified to use Robot's ways of doing console logging.

What I'm trying to do is, I need to be able to do console logging for logs coming from X when I run Robot Framework.

It seems like Robot doesn't catch logs coming from listeners (Although, it catches logs with levels more severe than 'INFO').. Example library:

class ExampleLibrary:
    ROBOT_LIBRARY_SCOPE = "GLOBAL"
    ROBOT_LIBRARY_DOC_FORMAT = "TEXT"
    ROBOT_LIBRARY_VERSION = "0.1"
    ROBOT_LISTENER_API_VERSION = 2

    def __init__(self, **kwargs) -> None:
        super().__init__(**kwargs)
        self.ROBOT_LIBRARY_LISTENER = self
        self._log = logging.getLogger("ExampleLogger")
        self._log.setLevel(logging.NOTSET)
        self._log.addHandler(logging.StreamHandler())
        self._log.info("Initialized ExampleLibrary")

    def _start_suite(self, test_suite, result) -> None:
        # We do calls to X here..
        # X creates its loggers and sends logs like the following line
        self._log.debug("suite started!")

    def _end_suite(self, test_suite, result):
        # We do calls to X here..
        # X creates its loggers and sends logs like the following line
        self._log.debug("suite ended!")

    def try_log(self):
        self._log.debug("trying debug log")
        self._log.warning("trying warning log")
        self._log.error("trying error log")

Example suite:

*** Settings ***
Library    ./ExampleLibrary.py

*** Test Cases ***
Passed Test
    TRY LOG

Trying to run the above suite.robot file using robot suite.robot :

Initialized FirstLibrary
==============================================================================
Suite                                                                         
==============================================================================
[ WARN ] trying warning log                                                   
[ ERROR ] trying error log                                                    
Passed Test                                                           | PASS |
------------------------------------------------------------------------------
Suite                                                                 | PASS |
1 test, 1 passed, 0 failed
==============================================================================

So, is there a way to make Robot Framework log messages coming from listeners ?

By default Robot hides any debug (and internal) level logging. You can try to enable it by using --loglevel DEBUG . You can see the available log levels in the relevant User Guide section . You can also enable the Robot internal function logging to see what's happening inside the framework provided libraries by setting the environment variable ROBOT_INTERNAL_TRACES to any value which is not empty.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM