【发布时间】:2021-06-27 19:55:01
【问题描述】:
如何在运行时使用重定向将 python 日志记录语句的输出重定向到文件?
我在 Debian 中使用 python 3.7。当我将包含打印语句的 python 脚本的输出重定向到一个文件时,我得到了预期的结果 - 一个包含输出的文件。
如果我使用 logging.debug 创建屏幕输出并将其重定向到一个文件,我会在屏幕上显示输出和一个零大小的文件。
python_test.py
print('hello')
python python_test.py > test.txt
more test.txt
hello
python_logging_test.py
import logging
logging.basicConfig(level=logging.DEBUG, format='%(message)s')
logging.debug('from logger')
python python_logging_test.py > test2.txt
from logger
ls -al test2.txt
-rw-r--r-- 1 bart bart 0 Mar 31 20:00 test2.txt
【问题讨论】: