![](/img/trans.png)
[英]How to print current logging configuration used by the python logging module?
[英]How to print over a current line, using the python logging module
我正在编写一个包含多个线程的程序,因此对于日志信息,我使用了python日志记录模块。 但是,我有部分应用程序,我想显示一个线程在同一行中等待执行多少次,每次都被覆盖。 我不希望在日志中显示多行。
我试图使用sys.stdout.flush()
函数。 不幸的是它不起作用。
我目前的代码:
def log_flush(message, start, stop):
import logging, sys
logging.basicConfig(level=logging.DEBUG,
format='(%(threadName)-10s) %(message)s',
)
time = stop - start
logging.debug((str(message) + str(time)))
sys.stdout.write('\r')
sys.stdout.flush()
for i in range(0, 5):
log_flush("Time taken: ", 0, i)
我的预期输出是(执行完成后):
(MainThread) Time taken: 4
目前脚本输出:
(MainThread) Time taken: 0
(MainThread) Time taken: 1
(MainThread) Time taken: 2
(MainThread) Time taken: 3
(MainThread) Time taken: 4
您可以print
退格键,如下所示:
import time, sys
t = 0
while True:
print('Seconds passed:', t, end='')
sys.stdout.flush()
time.sleep(1)
t += 1
print('\b'*20, end='')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.