Améliore log et page d'erreur 500.

This commit is contained in:
Emmanuel Viennet 2021-08-29 22:42:38 +02:00
parent 22475b1033
commit 3586920809
3 changed files with 87 additions and 43 deletions

View File

@ -14,7 +14,9 @@ from flask import request
from flask import Flask
from flask import current_app
from flask import g
from flask import has_request_context
from flask import render_template
from flask.logging import default_handler
from flask_sqlalchemy import SQLAlchemy
from flask_migrate import Migrate
from flask_login import LoginManager
@ -49,6 +51,25 @@ def handle_sco_value_error(exc):
return render_template("sco_value_error.html", exc=exc), 404
def internal_server_error(e):
# note that we set the 500 status explicitly
return render_template("error_500.html", SCOVERSION=sco_version.SCOVERSION), 500
class RequestFormatter(logging.Formatter):
"""Ajoute URL et remote_addr for logging"""
def format(self, record):
if has_request_context():
record.url = request.url
record.remote_addr = request.remote_addr
else:
record.url = None
record.remote_addr = None
return super().format(record)
def create_app(config_class=DevConfig):
app = Flask(__name__, static_url_path="/ScoDoc/static", static_folder="static")
app.logger.setLevel(logging.DEBUG)
@ -64,6 +85,7 @@ def create_app(config_class=DevConfig):
sco_cache.CACHE = cache
app.register_error_handler(ScoValueError, handle_sco_value_error)
app.register_error_handler(500, internal_server_error)
from app.auth import bp as auth_bp
@ -87,12 +109,16 @@ def create_app(config_class=DevConfig):
app.register_blueprint(
absences_bp, url_prefix="/ScoDoc/<scodoc_dept>/Scolarite/Absences"
)
# Config logs pour PRODUCTION
if not app.debug and not app.testing:
scodoc_exc_formatter = RequestFormatter(
"[%(asctime)s] %(remote_addr)s requested %(url)s\n"
"%(levelname)s in %(module)s: %(message)s"
)
if not app.testing:
if not app.debug:
# --- Config logs pour PRODUCTION
# On supprime le logguer par défaut qui va vers stderr et pollue les logs systemes
# nb: pas trouvé plus élégant
app.logger.handlers.pop()
app.logger.removeHandler(default_handler)
# --- Mail des messages ERROR et CRITICAL
if app.config["MAIL_SERVER"]:
auth = None
if app.config["MAIL_USERNAME"] or app.config["MAIL_PASSWORD"]:
@ -109,22 +135,21 @@ def create_app(config_class=DevConfig):
credentials=auth,
secure=secure,
)
mail_handler.setFormatter(scodoc_exc_formatter)
mail_handler.setLevel(logging.ERROR)
app.logger.addHandler(mail_handler)
else:
# Pour logs en DEV uniquement:
default_handler.setFormatter(scodoc_exc_formatter)
# Config logs pour DEV et PRODUCTION
if not app.testing:
# Configuration des logs (actifs aussi en mode development)
# usually /opt/scodoc-data/log/scodoc.log:
# rotated by logrotate
file_handler = WatchedFileHandler(
app.config["SCODOC_LOG_FILE"], encoding="utf-8"
)
file_handler.setFormatter(
logging.Formatter(
"%(asctime)s pid=%(process)d th=%(thread)s %(levelname)s: %(message)s " # "[in %(pathname)s:%(lineno)d]"
)
)
file_handler.setFormatter(scodoc_exc_formatter)
file_handler.setLevel(logging.INFO)
app.logger.addHandler(file_handler)

View File

@ -0,0 +1,18 @@
{% extends 'base.html' %}
{% import 'bootstrap/wtf.html' as wtf %}
{% block title %}Une erreur est survenue !{% endblock %}
{% block body %}
<h1>Une erreur est survenue !</h1>
<p>Oops... <span style="color:red;"><b>ScoDoc version <span style="font-size: 120%;">{{SCOVERSION}}</span></b></span> a
un problème, désolé.</p>
<p> Si le problème persiste, contacter l'administrateur de votre site,
ou écrire la liste "notes" <a href="mailto:notes@listes.univ-paris13.fr">notes@listes.univ-paris13.fr</a> en
indiquant la version du logiciel
(plus d'informations <a href="https://scodoc.org/ListesDeDiffusion/">sur les listes de diffusion</a>).
</p>
<p><a href="{{ url_for('scodoc.index') }}">retour à la page d'accueil</a></p>
{% endblock %}

View File

@ -65,16 +65,17 @@ def table_etud_in_accessible_depts():
# essais
@bp.route("/testlog")
def testlog():
import time
from flask import current_app
from app import log
# @bp.route("/testlog")
# def testlog():
# import time
# from flask import current_app
# from app import log
log(f"testlog called: handlers={current_app.logger.handlers}")
current_app.logger.debug(f"testlog message DEBUG")
current_app.logger.info(f"testlog message INFO")
current_app.logger.warning(f"testlog message WARNING")
current_app.logger.error(f"testlog message ERROR")
current_app.logger.critical(f"testlog message CRITICAL")
return "testlog completed at " + str(time.time())
# log(f"testlog called: handlers={current_app.logger.handlers}")
# current_app.logger.debug(f"testlog message DEBUG")
# current_app.logger.info(f"testlog message INFO")
# current_app.logger.warning(f"testlog message WARNING")
# current_app.logger.error(f"testlog message ERROR")
# current_app.logger.critical(f"testlog message CRITICAL")
# raise SyntaxError("une erreur de syntaxe")
# return "testlog completed at " + str(time.time())