Commit 25f10d27 authored by David Foucher's avatar David Foucher

Merge branch 'log-esd' into 'master'

Log esd

See merge request !59
parents e95e7382 cc89518f
Pipeline #3475 passed with stage
in 1 minute and 21 seconds
......@@ -68,7 +68,7 @@ async def simulate_(request, response):
await simulate(context, financements)
except DataError as err:
error = {err.key: err.error}
log_simulate(context, errors=error)
log_simulate(request, response, context, errors=error)
raise HttpError(HTTPStatus.BAD_REQUEST, error)
eligible = request.query.bool("eligible", None)
......@@ -93,7 +93,7 @@ async def simulate_(request, response):
body["scenario"] = make_scenario(context, financements)
response.json = body
log_simulate(context, financements=financements)
log_simulate(request, response, context, financements=financements)
# TODO : add pointer error for bad region number + test
......@@ -105,8 +105,8 @@ async def remuneration_(request, response):
await simulate_remuneration(context, remunerations)
except DataError as err:
error = {err.key: err.error}
log_simulate(context, errors=error)
raise HttpError(HTTPStatus.BAD_REQUEST, error)
log_simulate(request, response, context, errors=error)
raise HttpError(HTTPStatus.BAR_REQUEST, error)
# TODO: explain only for financement see routine.py check_remuneration
# explain = request.query.bool("explain", False)
......
......@@ -30,11 +30,11 @@ async def simulate_legacy(request, response):
await simulate(context, financements)
except DataError as err:
error = {err.key: err.error}
log_simulate(context, errors=error)
log_simulate(request, response, context, errors=error)
raise HttpError(HTTPStatus.BAD_REQUEST, error)
financements = [f for f in financements if f["eligible"]]
log_simulate(context, financements=financements)
log_simulate(request, response, context, financements=financements)
mapped = []
for financement in financements:
for tag, label in FAMILLE_MAPPING.items():
......
from datetime import datetime
import logging
import os
import uuid
import ujson as json
......@@ -21,8 +22,19 @@ request_logger.addHandler(
)
)
request_esd_logger = logging.getLogger("request_esd_logger")
request_esd_logger.setLevel(logging.INFO)
request_esd_logger.addHandler(
logging.FileHandler(
os.path.join(
os.environ.get("TREFLE_LOG_DIR", "/tmp"),
"statsesd_trefle.log",
),
)
)
def log_simulate(context, financements=None, errors=None):
def log_simulate(request, response, context, financements=None, errors=None):
financements = financements or []
errors = errors or []
message = {
......@@ -33,3 +45,19 @@ def log_simulate(context, financements=None, errors=None):
"version": VERSION,
}
request_logger.info(json.dumps(message, sort_keys=True))
application = request.headers.get('ORIGIN-USER')
if not application and request.headers.get('AUTHORIZATION'):
application = 'esd'
message_esd = {
"startup": "trefle",
"requestId": str(uuid.uuid4()),
"date": datetime.utcnow().isoformat(),
"remoteIP": request.headers.get("X-REAL-IP"),
"httpReferer": request.headers.get("REFERER"),
"httpUserAgent": request.headers.get("USER-AGENT"),
"status": response.status,
"apiVersion": VERSION,
"application": application,
}
request_esd_logger.info(json.dumps(message_esd, sort_keys=True))
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment