Pythonでファイルにログを書く方法は何ですか?

Pythonの場合、loggingモジュールを使用してファイルにログを書き込むことができます。以下は簡単な例です。

import logging

# 配置日志记录器
logging.basicConfig(filename='app.log', level=logging.INFO)

# 记录日志
logging.debug('This is a debug message')
logging.info('This is an info message')
logging.warning('This is a warning message')
logging.error('This is an error message')
logging.critical('This is a critical message')

上記の例では、logging.basicConfig()関数はログレコーダーを設定するために使用され、filenameパラメーターはログファイルのパスと名前を指定し、levelパラメーターはログのレベルを指定します(たとえば、logging.INFOはINFOレベル以上のログのみを記録します)。その後、logging.debug()、logging.info()などの関数を使用して、異なるレベルのログを記録することができます。

bannerAds