小编Inv*_*Zim的帖子

Python 日志记录,终止符作为选项

我想要的:在任何地方使用日志库而不是打印语句。有时最好不要以新行结束。考虑这个简化的例子:

for file in files:
    print('Loading {}'.format(file), end='', flush=True)
    try:
        data = load(file)
        print('\rLoaded {}'.format(file))
    except:
        print('\rFailed loading {}'.format(file))
Run Code Online (Sandbox Code Playgroud)

显而易见的方法是使用:

handler = logging.StreamHandler()
handler.terminator = ""
Run Code Online (Sandbox Code Playgroud)

但是,我不想向我的库添加处理程序,并且我确实希望主记录器的默认行为是以新行终止。以“”结尾感觉应该是例外,而不是规则。
有没有办法让我可以做类似的事情:

logger.info(msg, terminator="")
Run Code Online (Sandbox Code Playgroud)

无需为日志记录模块创建大量子类?

我对这个问题的看法是否合理,或者有更好的方法来处理这个问题吗?

python logging python-3.x

5
推荐指数
1
解决办法
2396
查看次数

标签 统计

logging ×1

python ×1

python-3.x ×1