aiohttp如何登录访问日志?

aiohttp如何登录访问日志?

问题描述:

我想获得aiohttp工作的基本记录器,但没有记录日志消息。 Note_日志自定义消息按预期工作。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) 
+0

我有一个类似的问题,并刚刚解决https://*.com/questions/43500983/specify-log-request-format-in-aiohttp-2/44482038#44482038 – Jacopofar

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

作为第一步,我建议设置根记录器,并在此之后下降到错误/访问日志。 也许访问自己的私人文件,如access.log日志。为了实现这一点,您需要为aiohttp.access记录器设置处理程序,而不是针对*aiohttp