2016-10-10 4 views
2

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://stackoverflow.com/questions/43500983/specify-log-request-format-in-aiohttp-2/44482038#44482038 – Jacopofar

답변

1

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이 필요합니다.

관련 문제