logging.basicConfig()関数でfilenameパラメーターを指定する。
■サンプルスクリプト
filelog.py
import logging logging.basicConfig(level=logging.DEBUG, format='%(asctime)s %(levelname)s %(lineno)s %(message)s', filename='mylog.log') logging.debug('デバッグ') logging.info('インフォ') logging.warning('ウォーン')
(実行結果)
$ python filelog.py
カレントディレクトリにmylog.logというファイルが作成される。
$ cat mylog.log 2017-09-04 00:56:16,800 DEBUG 7 デバッグ 2017-09-04 00:56:16,800 INFO 8 インフォ 2017-09-04 00:56:16,800 WARNING 9 ウォーン