首页 > 解决方案 > 使用 Python 的日志记录从 Robot Framework 侦听器进行控制台日志记录

问题描述

当前用例:假设我有一个名为 X 的独立 Python 库。

我想要做的是,当我运行 Robot Framework 时,我需要能够对来自 X 的日志进行控制台日志记录。

似乎 Robot 没有捕获来自侦听器的日志(尽管它捕获的日志级别比“INFO”更严重).. 示例库:

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")

示例套件:

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

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

尝试使用以下命令运行上述 suite.robot 文件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
==============================================================================

那么,有没有办法让 Robot Framework 记录来自侦听器的消息?

标签: pythonpython-3.xrobotframework

解决方案


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.


推荐阅读