如何在Python Quart路由处理程序中使用日志记录?

问题描述

我是hypercorn + uvloop + quart的新手。我正在尝试创建以下文件,并在路由处理程序中打印一些信息/调试日志,但未显示任何内容。我已经调试了路由处理程序,并注意到dog_server.logger.disabled = True。有谁知道这是什么问题?谢谢!

dog_blueprint.py

from quart import Blueprint
import logging

logging.basicConfig(level=logging.DEBUG)

class DogBlueprint(Blueprint):
    logger = None
    app_config = None

    def register(self,app,options,first_registration: bool = False):
        # app.logger.info('DogBlueprint is registering')
        print('Blueprint registering...')
        self.logger = app.logger 
        self.logger.info("Hello")  # This one working fine
        self.app_config = app.config
        super(DogBlueprint,self).register(app,first_registration)
        self.logger.info("World")  # This one working fine

route.py

dog_server = DogBlueprint('dog_server',__name__)
logging.basicConfig(level=logging.DEBUG)

@dog_server.route('/score',methods=['POST'])
async def post_handler():
    received = await _fetch_post_body(request)
    dog_server.logger.info(f'Received size: {len(received)}')  # This one does not work
    ... ... 

解决方法

这是Hypercorn中的错误,请参见此discussion。我会避免使用0.11.0并使用0.10.2或0.11.1。 (我是Hypercorn的作者)。