Я хочу настроить обработчик базы данных журналов, который будет сохранять всю информацию, которую Flask выводит на консоль (необработанные исключения, werkzeug, sqlachemy), а также мои собственные сообщения журналирования.
У меня проблемы с настройкой всего этого.Вопросы:
1) Почему имитируемая арифметическая ошибка в представлении about не регистрируется ни одним регистратором?
2) Почему нетрегистратор werkzeug правильно отображает загруженный сервер и выводит только «ошибка по запросу% s» для смоделированной арифметической ошибки.
3) Есть ли более простой способ просто передать все, что написано сервером Flask (и его компоненты) в БД?
Оцените ответы и любые другие предложения.
Что я сделал до сих пор:
models.py
class Log(DB.Model):
__tablename__ = 'logs'
id = DB.Column(DB.Integer, primary_key=True) # auto incrementing
logger = DB.Column(DB.String(100)) # the name of the logger. (e.g. myapp.views)
level = DB.Column(DB.String(100)) # info, debug, or error?
trace = DB.Column(DB.String(4096)) # the full traceback printout
msg = DB.Column(DB.String(4096)) # any custom log you may have included
created_at = DB.Column(DB.DateTime, default=DB.func.now()) # the current timestamp
def __init__(self, logger=None, level=None, trace=None, msg=None):
self.logger = logger
self.level = level
self.trace = trace
self.msg = msg
def __unicode__(self):
return self.__repr__()
def __repr__(self):
return "<Log: %s - %s>" % (self.created_at.strftime('%m/%d/%Y-%H:%M:%S'), self.msg[:50])
core.py
"""
This module contains the core objects of the application:
the Flask (APP) object and the database object.
"""
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_login import LoginManager
from samo.config import CONFIG_BY_NAME, ENVIRONMENT
APP = Flask(__name__)
APP.config.from_object('samo.config')
APP.config.from_object(CONFIG_BY_NAME[ENVIRONMENT])
DB = SQLAlchemy(APP)
DB.create_all()
LOGIN_MANAGER = LoginManager()
LOGIN_MANAGER.init_app(APP)
LOGIN_MANAGER.session_protection = "strong"
LOGIN_MANAGER.login_view = "auth.login"
run.py
from samo.core import APP as app, DB
from flask_wtf.csrf import CSRFProtect
from samo.config import config
from samo.models import Log
import traceback
import logging
class SQLAlchemyHandler(logging.Handler):
def emit(self, record):
trace = None
exc = record.__dict__['exc_info']
if exc:
trace = traceback.format_exc(exc)
log = Log(
logger=record.__dict__['name'],
level=record.__dict__['levelname'],
trace=trace,
msg=record.__dict__['msg'],)
DB.session.add(log)
DB.session.commit()
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
ch = SQLAlchemyHandler()
ch.setLevel(logging.INFO)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
ch.setFormatter(formatter)
loggers = [logger, logging.getLogger('werkzeug'), logging.getLogger('sqlalchemy'), logging.getLogger('flask.app')]
for l in loggers:
l.addHandler(ch)
csrf = CSRFProtect()
if __name__ == '__main__':
csrf.init_app(app)
logger.critical('TEST CRITICAL ERROR')
app.run(host=config['ENV']['HOST'])
просмотров.py
@APP.route('/about')
def about():
"""
Renders the about page.
:return: html template
"""
search = SearchForm(request.form)
raise ArithmeticError('A nasty error')
return render_template('about.html', postsearchform=search)
что было вставлено в базу данных
![enter image description here](https://i.stack.imgur.com/tsLBE.png)