Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/320.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python aiohttp如何记录访问日志?_Python_Aiohttp - Fatal编程技术网

Python aiohttp如何记录访问日志?

Python aiohttp如何记录访问日志?,python,aiohttp,Python,Aiohttp,我试图让一个基本的日志程序为aiohttp工作,但根本没有日志消息被记录。注uuu记录自定义消息按预期工作 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 curre

我试图让一个基本的日志程序为aiohttp工作,但根本没有日志消息被记录。注uuu记录自定义消息按预期工作

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\u格式
如果有,则应为“%s”。
“%a%l%u%t”%r“%s%b”%{referer}i“%{User Agent}i”
的有效参数。make\u handler(access\u log\u format=…)
调用,而不是
logging.Formatter

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

设置处理程序,只需导入日志并添加

logging.basicConfig(level=logging.DEBUG)

在你的run_应用程序调用之前的某个地方

我有一个类似的问题,刚刚解决