import logging # 设置默认的level为DEBUG # 设置log的格式 logging.basicConfig( level=logging.DEBUG, format="[%(asctime)s] %(name)s:%(levelname)s: %(message)s" ) # 记录log logging.debug(...) logging.info(...) logging.warn(...) logging.error(...) logging.critical(...)
上面的代码中,如果将level=logging.DEBUG这一行的日志打印级别调高,则源码中只有满足对应级别的调试信息才会打印出来。如下所示test.py:
import logging logging.basicConfig( level = logging.DEBUG, format = "[%(asctime)s] %(name)s:%(levelname)s: %(message)s" ) if __name__ == '__main__': logging.debug('hello word - debug') logging.info('hello word - info') logging.warn('hello word - warn') logging.error('hello word - error') logging.critical('hello word - critical')
倘若将logging级别更改为INFO,则只会打印INFO以上级别的信息