简体   繁体   English

使用 Python 的日志记录从机器人框架侦听器进行控制台日志记录

[英]Console logging from Robot Framework listeners using Python's logging

Current use case: Say I've a standalone Python library called X.当前用例:假设我有一个名为 X 的独立 Python 库。

  • I call X using my created Robot library that uses Robot listeners.我使用我创建的使用机器人侦听器的机器人库调用 X。
  • The X library I call uses Python's logging module to do logging.我调用的 X 库使用 Python 的 logging 模块进行日志记录。
  • X library shouldn't itself be modified to use Robot's ways of doing console logging.不应修改 X 库本身以使用 Robot 进行控制台日志记录的方式。

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.我想要做的是,当我运行 Robot Framework 时,我需要能够对来自 X 的日志进行控制台日志记录。

It seems like Robot doesn't catch logs coming from listeners (Although, it catches logs with levels more severe than 'INFO').. Example library:似乎 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")

Example suite:示例套件:

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

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

Trying to run the above suite.robot file using robot suite.robot :尝试使用robot suite.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 ?那么,有没有办法让 Robot Framework 记录来自侦听器的消息?

By default Robot hides any debug (and internal) level logging.默认情况下,Robot 会隐藏任何调试(和内部)级别的日志记录。 You can try to enable it by using --loglevel DEBUG .您可以尝试使用--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.您还可以通过将环境变量ROBOT_INTERNAL_TRACES设置为任何非空值来启用机器人内部函数日志记录,以查看框架提供的库中发生的情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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