Commit 26b32f32 authored by Julien Perrin's avatar Julien Perrin Committed by David Foucher

logs ESD

parent d0ab6df1
...@@ -68,7 +68,7 @@ async def simulate_(request, response): ...@@ -68,7 +68,7 @@ async def simulate_(request, response):
await simulate(context, financements) await simulate(context, financements)
except DataError as err: except DataError as err:
error = {err.key: err.error} error = {err.key: err.error}
log_simulate(context, errors=error) log_simulate(request, response, context, errors=error)
raise HttpError(HTTPStatus.BAD_REQUEST, error) raise HttpError(HTTPStatus.BAD_REQUEST, error)
eligible = request.query.bool("eligible", None) eligible = request.query.bool("eligible", None)
...@@ -93,7 +93,7 @@ async def simulate_(request, response): ...@@ -93,7 +93,7 @@ async def simulate_(request, response):
body["scenario"] = make_scenario(context, financements) body["scenario"] = make_scenario(context, financements)
response.json = body response.json = body
log_simulate(context, financements=financements) log_simulate(request, response, context, financements=financements)
# TODO : add pointer error for bad region number + test # TODO : add pointer error for bad region number + test
...@@ -105,8 +105,8 @@ async def remuneration_(request, response): ...@@ -105,8 +105,8 @@ async def remuneration_(request, response):
await simulate_remuneration(context, remunerations) await simulate_remuneration(context, remunerations)
except DataError as err: except DataError as err:
error = {err.key: err.error} error = {err.key: err.error}
log_simulate(context, errors=error) log_simulate(request, response, context, errors=error)
raise HttpError(HTTPStatus.BAD_REQUEST, error) raise HttpError(HTTPStatus.BAR_REQUEST, error)
# TODO: explain only for financement see routine.py check_remuneration # TODO: explain only for financement see routine.py check_remuneration
# explain = request.query.bool("explain", False) # explain = request.query.bool("explain", False)
......
...@@ -30,11 +30,11 @@ async def simulate_legacy(request, response): ...@@ -30,11 +30,11 @@ async def simulate_legacy(request, response):
await simulate(context, financements) await simulate(context, financements)
except DataError as err: except DataError as err:
error = {err.key: err.error} error = {err.key: err.error}
log_simulate(context, errors=error) log_simulate(request, response, context, errors=error)
raise HttpError(HTTPStatus.BAD_REQUEST, error) raise HttpError(HTTPStatus.BAD_REQUEST, error)
financements = [f for f in financements if f["eligible"]] financements = [f for f in financements if f["eligible"]]
log_simulate(context, financements=financements) log_simulate(request, response, context, financements=financements)
mapped = [] mapped = []
for financement in financements: for financement in financements:
for tag, label in FAMILLE_MAPPING.items(): for tag, label in FAMILLE_MAPPING.items():
......
...@@ -21,8 +21,18 @@ request_logger.addHandler( ...@@ -21,8 +21,18 @@ 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 [] financements = financements or []
errors = errors or [] errors = errors or []
message = { message = {
...@@ -33,3 +43,16 @@ def log_simulate(context, financements=None, errors=None): ...@@ -33,3 +43,16 @@ def log_simulate(context, financements=None, errors=None):
"version": VERSION, "version": VERSION,
} }
request_logger.info(json.dumps(message, sort_keys=True)) request_logger.info(json.dumps(message, sort_keys=True))
message_esd = {
"startup": "trefle",
"requestId": datetime.utcnow().isoformat(),
"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": request.headers.get("APPLI"),
}
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