How do you print logs in Python?
In Python, we can utilize the built-in logging module to print logs. Here is a simple example:
import logging
# 设置日志级别为DEBUG
logging.basicConfig(level=logging.DEBUG)
# 打印不同级别的日志
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')
When you run the code above, you will see different levels of log outputs on the console. By default, the log level is set to WARNING, so only logs of WARNING, ERROR, and CRITICAL levels will be displayed. You can change the log level by adjusting the level parameter in the basicConfig() function.