このコードがスクリーンに表示される理由はわかりませんが、ファイルには表示されません。ファイル "example1.log"が作成されますが、そこには何も書き込まれません。PythonのbasicConfigメソッドを使用してコンソールとファイルにログする
#!/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')
私は、ログオブジェクト(example code)を作成することで、この問題を「バイパス」しているが、それはにbasicConfig()のアプローチが失敗した理由私を悩ませ続けて?
PS。私はにbasicConfig呼び出しを変更する場合:
logging.basicConfig(level=logging.DEBUG,
filename="example2.log",
format='%(asctime)s %(message)s',
handlers=[logging.StreamHandler()])
はその後、すべてのログがファイルにあり、何もコンソールに表示されていない
python 2.7では、basicConfigが理解するキーワード引数は、 "filename"、 "filemode"、 "stream"、 "format"、 "datefmt"、および "level"です。 "ハンドラー"は何もしません。 – TocToc