2016-10-10 23 views
2

私はaiohttpの作業のための基本的なロガーを取得しようとしていますが、単にログされているログメッセージはありません。注:カスタムメッセージのログは期待通りに機能します。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ではありません。

関連する問題