specify log request format in aiohttp 2

前端 未结 1 1242
旧时难觅i
旧时难觅i 2021-01-23 05:29

I\'m using aiohttp 2 with Python 3.6 and want to log the requests coming to the application.

I did:

# use ISO timestamps
from time import gmtime
logging.         


        
相关标签:
1条回答
  • 2021-01-23 05:37

    You can see my sample:

    import asyncio
    import logging
    
    from aiohttp import web
    
    
    mylogger = logging.getLogger('aiohttp.access')
    mylogger.setLevel(logging.DEBUG)
    ch = logging.StreamHandler()
    mylogger.addHandler(ch)
    
    
    async def handle(request):
        name = request.match_info.get('name', "Anonymous")
        text = "Hello, " + name
        return web.Response(text=text)
    
    loop = asyncio.get_event_loop()
    
    app = web.Application(loop=loop)
    app.router.add_get('/', handle)
    app.router.add_get('/{name}', handle)
    
    loop.run_until_complete(
        loop.create_server(
            app.make_handler(access_log=mylogger,
                             access_log_format='%r %s %b'), '0.0.0.0', 8080))
    loop.run_forever()
    loop.close()
    

    run it and access 'http://127.0.0.1:8080/xmwd', you will see GET /xmwd HTTP/1.1 200 11 in your console.

    0 讨论(0)
提交回复
热议问题