可以使用Python内置的logging模块来记录日志,可以通过设置日志等级来控制记录日志的详细程度。以下是示例代码:
import logging
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s %(name)-12s %(levelname)-8s %(message)s', datefmt='%m-%d %H:%M', filename='myapp.log', filemode='w')
logger = logging.getLogger('example')
logger.debug('This is a debug message') logger.info('This is an info message') logger.warning('This is a warning message') logger.error('This is an error message') logger.critical('This is a critical message')
当日志等级设置为DEBUG时,所有级别的日志都将被记录下来,如果需要记录更详细的信息,可以在代码中添加更多的logger.debug等级的记录。