python - 为 flask 记录器设置数据库处理程序

标签 python python-3.x flask

我希望设置一个日志数据库处理程序,它将保存 Flask 当前输出到控制台的所有信息(未处理的异常、werkzeug、sqlachemy),以及我的自定义日志消息。

我在设置这一切时遇到了麻烦。问题:

1) 为什么任何记录器都没有发现 about View 中模拟的算术错误?

2) 为什么 werkzeug 记录器没有正确显示加载的服务器,并且对于模拟的 Aritmetic 错误只有输出“请求 %s 时出错”。

3)有没有更简单的方法可以将 Flask 服务器(及其组件)写入的所有内容通过管道传输到数据库?

感谢回复和任何其他建议。

到目前为止我所做的:

模型.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"

运行.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'])

View .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

最佳答案

万一有人偶然发现这个

在 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() ##CHANGE HERE, removed exc parameter
        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'])

问题解决了。

关于python - 为 flask 记录器设置数据库处理程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52728928/

相关文章:

python - 如何使用 python 从 SNMP 获取数据?

python - 重定向到带有 header 的 OAuth URL

python - Flask 可以单独运行在 Gunicorn 上吗?

python - 在没有 HTML 的情况下将网站图标添加到 Flask 服务器

python - 多索引数据帧的索引排序顺序不遵守分类索引顺序

python - 处理 ZeroDivisionError 的最佳方法?

python - 加载 EMNIST-letters 数据集

python-3.x - 获取 ABCMeta 的所有注册子类

python - 我的 OpenCV remap()ing 有什么问题?

python - 设置 WingIDE 以调试 Flask 项目