aiohttp如何记录访问日志?

编程入门 行业动态 更新时间:2024-10-26 13:31:45
本文介绍了aiohttp如何记录访问日志?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试使aiohttp正常工作,但是没有日志消息被记录下来。注意,记录自定义消息的工作符合预期。

I am trying to get a basic logger for aiohttp working, but there are simply no log messages being logged. Note_ logging custom messages works as expected.

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 的有效参数。

LOG_FORMAT should be "%s" if any. '%a %l %u %t "%r" %s %b "%{Referrer}i" "%{User-Agent}i"' is a valid parameter for .make_handler(access_log_format=...) call, not logging.Formatter.

第一步,我建议设置root logger,然后转到错误/访问日志。 也许值得拥有私人文件的访问日志,例如 access.log 。为此,您需要为 aiohttp.access 记录器设置处理程序,而不是为顶级 aiohttp 设置处理程序。

As first step I suggest setting up root logger and after that going down to error/access logs. Perhaps access log worth own private file like access.log. To achieving this you need to setup a handler for aiohttp.access logger, not for top-level aiohttp.

更多推荐

aiohttp如何记录访问日志?

本文发布于:2023-11-23 10:18:34,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1621088.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:日志   aiohttp

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!