このコードが画面に印刷されるのにファイルには印刷されない理由がわかりませんか?ファイル「example1.log」が作成されますが、何も書き込まれません。
#!/usr/bin/env python3
import logging
logging.basicConfig(level=logging.DEBUG,
format='%(asctime)s %(message)s',
handlers=[logging.FileHandler("example1.log"),
logging.StreamHandler()])
logging.debug('This message should go to the log file and to the console')
logging.info('So should this')
logging.warning('And this, too')
ロギングオブジェクトを作成することでこの問題を「バイパス」しましたが、basicConfig()
アプローチが失敗した理由を悩ませ続けています。
PS。basicConfig呼び出しを次のように変更した場合:
logging.basicConfig(level=logging.DEBUG,
filename="example2.log",
format='%(asctime)s %(message)s',
handlers=[logging.StreamHandler()])
その後、すべてのログがファイルにあり、コンソールには何も表示されません。