在Python中,可以使用logging模块来写log。以下是一个简单的例子:pythonimport logging# 配置log的格式logging.basicConfig(level=logging.DEBUG, # 设置log级别为DEBUGformat='%(asctime)s - %(levelname)s - %(message)s',filename='app.log', # 指定log文件名filemode='w' # 设置写入模式,'w'表示每次重新写入log文件) # 写loglogging.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')在运行以上代码后,会生成一个名为app.log的文件,其中包含了写入的log信息。

Python日志记录:使用logging模块写日志

原文地址: https://www.cveoy.top/t/topic/pWFj 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录