[英]How to silence print messages while executing a code using trace library?
我正在使用跟蹤庫跟蹤代碼。 在執行其他代碼時,我在屏幕上收到了一些存在的打印消息。 我怎樣才能讓他們沉默?
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) 的內容仍應打印。
如果您使用的是其他操作系統,您應該為此尋找一些東西。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.