1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Python3でログファイルに出力しつつ、コンソール画面にも表示する(basicConfigを使用)

Last updated at Posted at 2021-01-26

Python3でlogging × basicConfigを使用してログファイルに出力しつつ、画面にも表示したい場合は、logging.basicConfigでhandlersを使う(要Python3.3以上)。

import logging
from time import strftime

# ログ設定
logformat = '%(asctime)s : %(levelname)s : %(message)s'
logfile = 'hoge_{}.log'.format(strftime('%Y%m%d'))

logging.basicConfig(level=logging.INFO, format=logformat, 
                    handlers=[logging.FileHandler(logfile), logging.StreamHandler()])

# ログ出力
logging.info('info %s %s', 'hoge', 'fuga')

logging.basicConfig(filename='xxxx.log')と一緒に使うとエラーになるので注意。

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?