#日志级别 : noset debug info warning error critical #控制台默认的级别是warning的。 import logging logging.basicConfig(level=logging.WARN, format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',datefmt=' %Y/%m/%d %H:%M:%S', filename='myapp.log', filemode='a') logger = logging.getLogger(__name__) def hello(): print('hello world') def main(): logger.info('开始执行main函数') hello() logger.info('执行完毕') try: a = 2/0 except Exception as e: logger.error('0不能作为除数') if __name__ == '__main__': main()