温馨提示×

python怎么写日志到文件

小亿
192
2024-01-12 23:44:16
栏目: 编程语言

在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()等函数来记录不同级别的日志。

0