У меня есть aiohttp
веб-приложение, которое использует маршруты как фляги, как декораторы, и gunicorn
У меня возникли проблемы с корректной работой журналов.
Чего мне здесь не хватает?
Никаких ошибок не выдается и не регистрируется, и приложение работает нормально, но ничего не регистрируется, кроме журналов запуска:
[2018-10-16 09:41:18 +0000] [1] [INFO] Starting gunicorn 19.9.0
[2018-10-16 09:41:18 +0000] [1] [INFO] Listening at: http://0.0.0.0:8080 (1)
[2018-10-16 09:41:18 +0000] [1] [INFO] Using worker: aiohttp.worker.GunicornWebWorker
[2018-10-16 09:41:18 +0000] [16] [INFO] Booting worker with pid: 16
[2018-10-16 09:41:18 +0000] [17] [INFO] Booting worker with pid: 17
[2018-10-16 09:41:18 +0000] [18] [INFO] Booting worker with pid: 18
[2018-10-16 09:41:18 +0000] [19] [INFO] Booting worker with pid: 19
[2018-10-16 09:41:18 +0000] [20] [INFO] Booting worker with pid: 20
[2018-10-16 09:41:18 +0000] [21] [INFO] Booting worker with pid: 21
[2018-10-16 09:41:18 +0000] [22] [INFO] Booting worker with pid: 22
[2018-10-16 09:41:18 +0000] [23] [INFO] Booting worker with pid: 23
Мое приложение /Файл init.py выглядит так:
import logging
import os
from logging import handlers
from aiohttp.web import Application
from app.routes import routes
from utils.logging import CloggerFormatter
def create_app(app_config):
app = Application()
logger = logging.getLogger('aiohttp.web')
log_level = logging.DEBUG
if os.environ.get('LOG_LEVEL'):
log_level = os.environ['LOG_LEVEL']
app.router.add_routes(routes)
logger.setLevel(log_level)
logger.addHandler(CloggerFormatter)
app['config'] = app_config
return app
А затем в моем файле app / rout.py я получаю доступ к логгеру с помощью request.app.logger
из определения маршрута, например:
from aiohttp.web import Response, RouteTableDef
routes = RouteTableDef()
@routes.post('/background-checks')
async def api_background_check(request):
request_identifier = request.headers.get('X-Request-ID')
if not request_identifier:
request_identifier = uuid.uuid4()
request.app.logger.info('Checking background for request: %s', request_identifier)
Это мой файл utils / handlers / logging.py:
from time import strftime, gmtime
from logging import Formatter
class CloggerFormatter(Formatter):
"""
Logging module formatter in accordance with the yoti clogger manual
guidelines.
"""
converter = gmtime
def __init__(self, datefmt=None):
fmt = ('level:%(levelname)s'
'\ttime:%(asctime)s'
'\tmessage:%(message)s')
Formatter.__init__(self, fmt=fmt, datefmt=datefmt)
def formatTime(self, record, datefmt=None):
"""
Return the creation time of the LogRecord using the RFC 3339
format if datefmt is not specified.
"""
ct = self.converter(record.created)
if datefmt:
s = strftime(datefmt, ct)
else:
t = strftime('%Y-%m-%dT%H:%M:%S', ct)
s = '%s.%03dZ' % (t, record.msecs)
return s