Ipython Tee没有捕获日志记录输出

时间:2020-04-23 20:56:40

标签: python logging ipython

运行下面的代码,我在标准输出中看到日志“ info”和“ warning”行以及打印行。但是只有“打印”行会转到我的console.log文件。有人知道我怎样才能将所有内容保存到文件中吗?这是ubuntu中的python 3.6.4。

from IPython.utils.io import Tee
import tempfile, os, sys, logging

logging.getLogger().handlers=[]
logging.basicConfig(stream=sys.stdout, level=logging.INFO, format='%(asctime)s %(name)s %(message)s')
LOG = logging.getLogger('test')

console_fname = os.path.join(tempfile.gettempdir(), "console.log")
t = Tee(console_fname, channel='stdout')
print('hello')
LOG.info('info')
LOG.warning('warning')
print('goodbye')
t.close()

with open(console_fname, 'r') as f:
    print('\nfile contents:\n%s'%f.read())

0 个答案:

没有答案