发现问题
前几天写了一个项目,需要用到paddlex包中的OCR检测产线。于是我pip install paddlex
导入paddlex,在create_pipeline
后,很惊讶的发现我在当前文件自定义的logging
竟然不输出东西了!
测试问题
于是我写了一个小demo来验证这个问题。代码如下:
import logging
import coloredlogs
from paddlex import create_pipeline
logger = logging.getLogger(__name__)
coloredlogs.install(level="INFO")
if __name__ == "__main__":
logger.info(111)
pipeline = create_pipeline(pipeline="seal_recognition")
logger.info(222)
尝试运行后问题就出现了,logger.info("222")
居然被吞了!
思考问题
那么究竟是什么原因导致的这个问题呢,我们进入paddlex/utils
包中发现其存在重写的logging.py
文件,paddlex
这玩意一定有东西和我代码的logging
有冲突。
那么,如何验证这个冲突呢?
解决问题
我写了一个demo测试了一下该问题!
import logging
import coloredlogs
from paddlex import create_pipeline
def debug_logger_config():
root = logging.getLogger()
current = logging.getLogger(__name__)
print(f"[Root] Level: {root.level}, Handlers: {root.handlers}")
print(f"[Current] Level: {current.level}, Effective Level: {current.getEffectiveLevel()}, Handlers: {current.handlers}")
logger = logging.getLogger(__name__)
coloredlogs.install(level="INFO")
print("Before create_pipeline:")
debug_logger_config()
logger.info(111)
pipeline = create_pipeline(pipeline="seal_recognition")
print("\nAfter create_pipeline:")
debug_logger_config()
logger.info(222)
运行后输出
Before create_pipeline:
[Root] Level: 20, Handlers: [<StandardErrorHandler <stderr> (INFO)>]
[Current] Level: 0, Effective Level: 20, Handlers: []
2025-02-20 08:30:52 XXZX-Cheny __main__[21664] INFO 111
Using official model (RT-DETR-H_layout_3cls), the model files will be be automatically downloaded and saved in C:\Users\cheny\.paddlex\official_models.
Using official model (PP-OCRv4_server_seal_det), the model files will be be automatically downloaded and saved in C:\Users\cheny\.paddlex\official_models.
Using official model (PP-OCRv4_server_rec), the model files will be be automatically downloaded and saved in C:\Users\cheny\.paddlex\official_models.
After create_pipeline:
[Root] Level: 30, Handlers: [<StandardErrorHandler <stderr> (INFO)>] # 被重置
[Current] Level: 0, Effective Level: 30, Handlers: [] # 当前Level等级为30,也就是只输出Warning以上的日志
我们发现,在这个demo中,create_pipeline修改了根日志配置,将日志输出等级重置为warning
的30
,超出了info
等级的20
。然而,我们本地文件日志输出等级是20
,所以就无法输出现有日志了!
解决这个问题非常简单,那就是需要你在当前文件手动加入logger.setLevel(logging.INFO)
,显式绑定到记录器后问题解决。样例如下:
logger = logging.getLogger(__name__)
coloredlogs.install(level="INFO")
logger.setLevel(logging.INFO)
此时,我们再尝试一下运行demo后输出
Before create_pipeline:
[Root] Level: 20, Handlers: [<StandardErrorHandler <stderr> (INFO)>]
[Current] Level: 20, Effective Level: 20, Handlers: []
2025-02-20 08:37:27 XXZX-Cheny __main__[33700] INFO 111
Using official model (RT-DETR-H_layout_3cls), the model files will be be automatically downloaded and saved in C:\Users\cheny\.paddlex\official_models.
Using official model (PP-OCRv4_server_seal_det), the model files will be be automatically downloaded and saved in C:\Users\cheny\.paddlex\official_models.
Using official model (PP-OCRv4_server_rec), the model files will be be automatically downloaded and saved in C:\Users\cheny\.paddlex\official_models.
After create_pipeline:
[Root] Level: 30, Handlers: [<StandardErrorHandler <stderr> (INFO)>] # 被重置
[Current] Level: 20, Effective Level: 20, Handlers: [] # 由于显式绑定,所以没有改变
2025-02-20 08:37:45 XXZX-Cheny __main__[33700] INFO 222 #
后记
该问题已经被我反馈到PaddleX仓库,具体Issue链接如下:https://github.com/PaddlePaddle/PaddleX/issues/3402