11,877
社区成员




在调试程序时,发现自定义的logging能输出到文件,但是无法打印到屏幕。试了setLevel等都没用,但神奇的发现只要调用过语句logging.info后,信息就可以打印到屏幕。
将代码提取出来,问题很容易复现。
import logging
my_logger = logging.getLogger("relay")
my_logger.setLevel(logging.INFO)
my_logger.info("why the info cannot be output to the console")
logging.info("lalala")
my_logger.info("the info can be output to the console after calling logging.info")
print("why why why")
在vs code调试输出如下,调用logging.info("lalala") 之前的my_logger.info("why the info...")没有打印到屏幕,但是调用 过后的my_logger.info("the info ...")就输出到了屏幕上。百思不得其解,望高手指点!
需要配置自定义logger的handler
文档
https://docs.python.org/zh-cn/3/library/logging.html
flush()