个人项目导入PaddleX后logging日志打印在初始化create_pipeline后无法输出

发现问题

前几天写了一个项目,需要用到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修改了根日志配置,将日志输出等级重置为warning30,超出了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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ChairC

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值