python - aiohttp如何记录访问日志?

标签 python aiohttp

我正在尝试让 aiohttp 工作的基本记录器,但根本没有记录任何日志消息。注意_记录自定义消息按预期工作。

async def main_page(request: web.Request):
    return "hello world"

def setup_routes(app):
    app.router.add_get('/', main_page)


async def init(loop):
    # load config from yaml file in current dir
    conf = load_config(str(pathlib.Path('.') / 'async_config.yml'))

    # setup application and extensions
    app = web.Application(loop=loop)

    # setup views and routes
    setup_routes(app)

    host, port = conf['host'], conf['port']

    app['gmt_file'] = _get_gmt_file()

    return app, host, port

    LOG_FORMAT = '%a %l %u %t "%r" %s %b "%{Referrer}i" "%{User-Agent}i"'
    log_file = "log.text"
    handler = handlers.TimedRotatingFileHandler(log_file, when='midnight',
                                                backupCount=5)

    handler.setLevel(logging.DEBUG)
    formatter = logging.Formatter(LOG_FORMAT)
    handler.setFormatter(formatter)
    handler.name = "file_log"

    loop = asyncio.get_event_loop()
    app, host, port = loop.run_until_complete(init(loop))

    logging.getLogger("aiohttp").addHandler(handler)

    # todo host ziehen per aiohttp methods, so we are externally visible.
    web.run_app(app, host=host, port=port)

最佳答案

LOG_FORMAT 如果有的话应该是“%s”。 '%a %l %u %t "%r"%s %b "%{Referrer}i""%{User-Agent}i"' 的有效参数。 make_handler(access_log_format=...) 调用,而不是 logging.Formatter

作为第一步,我建议设置根记录器,然后进入错误/访问日志。 也许访问日志值得自己的私有(private)文件,如 access.log。要实现这一点,您需要为 aiohttp.access 记录器设置一个处理程序,而不是为顶级 aiohttp

关于python - aiohttp如何记录访问日志?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39956400/

相关文章:

python - 问题 azure 测试聊天机器人 "Error while finding module specification for ' aiohttp.web' (ModuleNotFoundError : No module named 'aiohttp' )

python - 我可以为整个模块使用上下文管理器吗?

javascript - 如何在 django 模板中创建多个折叠功能

python - 表单提交而不是 POST 是 Django 中的值

python - Pythonic 是什么意思?

python - 逐步创建异步任务并等待所有任务完成

python - 为 aiohttp 中的静态文件启用 gzip 压缩

python - 使用 asyncio 发出 100 个请求后,并行请求会无限阻塞

python - DRF - 是否可以将 URL 中的多个过滤器参数与某种 OR 逻辑符号结合起来

python - GSpread 从 Python 而非 JSON 传递凭证