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 1 year has passed since last update.

logging 練習

Last updated at Posted at 2022-01-27

logging 練習

from logging import ERROR, Formatter, Handler, getLogger, FileHandler, Formatter, DEBUG

# loggerを作る
logger = getLogger(__name__)

# 手下のhandlerを作る(手下:handlerには属性があり、filehandler:ファイル作るやつ、とstreamhandler:コンソールに出すやつがいる)
handler = FileHandler('log.txt',encoding='utf-8')

# loggerに手下を割り当てる
logger.addHandler(handler)

# 手下の報告書フォーマット決める
# reportForm = Formatter('[%(levelname)s] %(asctime)s - %(message)s (%(filename)s)')
reportForm = Formatter('%(levelname)s,%(asctime)s,%(message)s,%(filename)s')

# 報告書フォーマットでhandlerに指示する
handler.setFormatter(reportForm)

# 手下の報告基準を設定する
handler.setLevel(DEBUG)

# loggerの報告基準を設定する
logger.setLevel(DEBUG)


logger.debug('てすと')
logger.error('えらー')
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?