Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add backend logging configuration #304

Merged
merged 2 commits into from
Jun 30, 2022
Merged

Add backend logging configuration #304

merged 2 commits into from
Jun 30, 2022

Conversation

florimondmanca
Copy link
Collaborator

@florimondmanca florimondmanca commented Jun 27, 2022

Closes #186

TODO:

  • Tester sur staging

Cette PR configure le serveur backend pour afficher les logs en format "humain" dans la console en développement, et en JSON en production (plus facile à éventuellement traiter).

$ make serve-server
[server] 2022-06-27 17:10:07,744 INFO:     uvicorn.error: Will watch for changes in these directories: ['/home/florimond/dev/prj-catalogue/catalogage-donnees/server']
[server] 2022-06-27 17:10:07,745 INFO:     uvicorn.error: Uvicorn running on http://localhost:3579 (Press CTRL+C to quit)
[server] 2022-06-27 17:10:07,758 INFO:     uvicorn.error: Started reloader process [146526] using watchgod
[server] 2022-06-27 17:10:08,393 INFO:     uvicorn.error: Started server process [146561]
[server] 2022-06-27 17:10:08,393 INFO:     uvicorn.error: Waiting for application startup.
[server] 2022-06-27 17:10:08,393 INFO:     uvicorn.error: Application startup complete.
[server] 2022-06-27 17:10:20,550 INFO:     uvicorn.access: 127.0.0.1:54132 - "GET /docs HTTP/1.1" 200
[server] 2022-06-27 17:10:20,680 INFO:     uvicorn.access: 127.0.0.1:54132 - "GET /openapi.json HTTP/1.1" 200
$ APP_SERVER_MODE=live make serve-server
./tools/colorize_prefix.sh [server] 34 "venv/bin/python -m server.main"
[server] {"asctime": "2022-06-27 17:18:14,388", "levelname": "INFO", "name": "uvicorn.error", "message": "Started server process [148892]"}
[server] {"asctime": "2022-06-27 17:18:14,388", "levelname": "INFO", "name": "uvicorn.error", "message": "Waiting for application startup."}
[server] {"asctime": "2022-06-27 17:18:14,388", "levelname": "INFO", "name": "uvicorn.error", "message": "Application startup complete."}
[server] {"asctime": "2022-06-27 17:18:14,389", "levelname": "INFO", "name": "uvicorn.error", "message": "Uvicorn running on http://localhost:3579 (Press CTRL+C to quit)"}
[server] {"asctime": "2022-06-27 17:18:30,292", "levelname": "INFO", "name": "uvicorn.access", "message": "127.0.0.1:54134 - \"GET /api/docs HTTP/1.1\" 404"}

Une fois mergé, on pourra ajouter nos propres logs (utile pour le debugging) avec:

import logging

logger = logging.getLogger(__name__)

logger.debug("This is a debug line...")
logger.debug("Another debug line", extra={"value": "Some extra value"})

Références qui m'ont servi :

@florimondmanca florimondmanca force-pushed the fm/server-logging branch 4 times, most recently from 2acc193 to bdfe47b Compare June 27, 2022 15:19
@florimondmanca florimondmanca changed the title Add backend JSON logging configuration Add backend logging configuration Jun 27, 2022
@florimondmanca
Copy link
Collaborator Author

florimondmanca commented Jun 30, 2022

Validation staging

J'ai déployé puis inspecté les fichiers de logs.

/var/log/server.out.log : le nouveau format JSON est utilisé

$ tail /var/log/server.out.log
INFO:     86.247.59.154:0 - "GET /datasets/?page_number=1&page_size=50&q= HTTP/1.0" 200 OK
INFO:     86.247.59.154:0 - "GET /datasets/filters/ HTTP/1.0" 422 Unprocessable Entity
INFO:     86.247.59.154:0 - "GET /datasets/?page_number=1&page_size=50&q= HTTP/1.0" 200 OK
INFO:     86.247.59.154:0 - "GET /auth/check/ HTTP/1.0" 200 OK
{"asctime": "2022-06-30 10:36:39,682", "levelname": "INFO", "name": "uvicorn.error", "message": "Started server process [2978686]"}
{"asctime": "2022-06-30 10:36:39,682", "levelname": "INFO", "name": "uvicorn.error", "message": "Waiting for application startup."}
{"asctime": "2022-06-30 10:36:39,683", "levelname": "INFO", "name": "uvicorn.error", "message": "Application startup complete."}
{"asctime": "2022-06-30 10:36:39,686", "levelname": "INFO", "name": "uvicorn.error", "message": "Uvicorn running on http://localhost:3579 (Press CTRL+C to quit)"}

/var/log/server.err.log : on voit l'ancien format de logs pour l'arrêt du serveur (suite au redémarrage lié au déploiement), mais pas les nouveaux logs pour le démarrage du serveur : c'est attendu, car tout le logging est dirigé vers stdout. On voit les logs de démarrage ci-dessus.

$ tail /var/log/server.err.log
sqlalchemy.exc.InterfaceError: (sqlalchemy.dialects.postgresql.asyncpg.InterfaceError) <class 'asyncpg.exceptions._base.InterfaceError'>: connection is closed
[SQL: SELECT "user".id, "user".email, "user".password_hash, "user".role, "user".api_token 
FROM "user" 
WHERE "user".api_token = %s]
[parameters: ('038e69079a0e4a5adfc57e6aac79eca696db77ec3eb02fb527fb821f52ab64cb',)]
(Background on this error at: https://sqlalche.me/e/14/rvf5)
INFO:     Shutting down
INFO:     Waiting for application shutdown.
INFO:     Application shutdown complete.
INFO:     Finished server process [2931886]

@florimondmanca florimondmanca merged commit 605a7f2 into master Jun 30, 2022
@florimondmanca florimondmanca deleted the fm/server-logging branch June 30, 2022 10:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Logging HTTP backend
1 participant