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



我正在使用跟踪库跟踪代码。在执行其他代码时,我在屏幕上收到一些存在存在的打印消息。我怎样才能让他们沉默?

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语句,如下所示。

这是我在屏幕上看到的内容:

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 .

不需要的行除了最后一行之外都是。

logging库使用STDERR来执行日志记录,而print()使用STDIN

也就是说,您可以重定向输出以仅获取打印到STDERR的内容(假设其他垃圾打印是打印到 STDIN(。

在 Linux 中,你会做这样的事情:

python my_program.py >/dev/null

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

如果您使用的是其他操作系统,则应代表此查找某些内容。

最新更新