如何在使用跟踪库执行代码时使打印消息静音?

问题描述 投票:0回答:1

我正在使用trace库跟踪代码。在执行其他代码时,我在屏幕上看到了一些打印消息。我如何使他们沉默?

def generate_sequential_function_calls(self):
    """generate sequential function calls
    for tracing source code and plotting sequence diagram.
    """
    # generating sequence diagram for a use-case
    _ = GenerateSequenceDiagram(
        self.driver_path, self.driver_name, self.source_code_path[0])
    spec = importlib.util.spec_from_file_location(
        self.driver_name, self.driver_path)
    global foo
    foo = self.foo
    foo = importlib.util.module_from_spec(spec)
    spec.loader.exec_module(foo)
    tracer = Trace(countfuncs=1, countcallers=1, timing=1)
    tracer.run('foo.{}()'.format(self.driver_function))
    results = tracer.results()
    caller_functions = results.callers
    function_sequence = []  # consists of all functions called in sequence
    for caller, callee in caller_functions:
        _, caller_module, caller_function = caller
        _, callee_module, callee_function = callee
        if caller_module not in self.source_code_modules or callee_module not in self.source_code_modules:
            logging.debug(
                "Following modules are not in source code and thus to be ignored:")
            logging.debug(caller_module)
            continue
        function_sequence.append(
            [(caller_module, caller_function), (callee_module, callee_function)])
    logging.debug("Function sequence is: ")
    for sequence in function_sequence:
        logging.debug(sequence)

我尝试用我自己的代码设置日志记录级别,但是徒劳。

执行的代码具有正常的print语句,如here所示。

这是我在屏幕上看到的:

Inside main_2 func
False
True
The dataframe is:
   0
0  1
1  2
2  3
3  4
INFO:root:docker cp gruml://home/ubuntu/generate_uml/Use_Case_test_cliDependency_2.xlsx .

不需要的行只剩下最后一行。

python python-3.x trace
1个回答
0
投票

[logging库使用STDERR进行记录,而print()使用STDIN

也就是说,您可以重定向输出以仅将打印的内容输出到STDERR(假设其他垃圾打印内容是按STDIN进行的。)>

在Linux中,您将执行以下操作:

python my_program.py >/dev/null

这会将文件描述符

1的输出重定向到/ dev / null(I / O的黑洞)。因此,仍应打印正在打印到文件描述符2(STDERR)的内容。

如果您使用的是其他操作系统,则应代表该对象寻找内容。

© www.soinside.com 2019 - 2024. All rights reserved.