zoukankan      html  css  js  c++  java
  • flask logging 最佳实践

    flask项目中, 你可以使用python 的 logging模块实现记录日志. 也可以使用 flask 基于logging模块封装过的app.logger实现.

    直接上代码

    config.py

    import os
    import logging


    basedir = os.path.abspath(os.path.dirname(__file__))


    class InfoFilter(logging.Filter):
    def filter(self, record):
    """only use INFO
    筛选, 只需要 INFO 级别的log
    :param record:
    :return:
    """
    if logging.INFO <= record.levelno < logging.ERROR:
    # 已经是INFO级别了
    # 然后利用父类, 返回 1
    return super().filter(record)
    else:
    return 0


    class Config:
    SECRET_KEY = os.environ.get('SECRET_KEY') or 'hard to guess string'
    SSL_DISABLE = False
    SQLALCHEMY_RECORD_QUERIES = True

    LOG_PATH = os.path.join(basedir, 'logs')
    LOG_PATH_ERROR = os.path.join(LOG_PATH, 'error.log')
    LOG_PATH_INFO = os.path.join(LOG_PATH, 'info.log')
    LOG_FILE_MAX_BYTES = 100 * 1024 * 1024
    # 轮转数量是 10 个
    LOG_FILE_BACKUP_COUNT = 10

    @staticmethod
    def init_app(app):
    pass


    class DevelopmentConfig(Config):
    DEBUG = True
    PRESERVE_CONTEXT_ON_EXCEPTION = False
    SQLALCHEMY_DATABASE_URI = 'mysql+pymysql://xxx:xxx@127.0.0.1/xxx?charset=utf8'

    @classmethod
    def init_app(cls, app):
    Config.init_app(app)

    # email errors to the administrators
    import logging
    from logging.handlers import RotatingFileHandler
    # Formatter
    formatter = logging.Formatter(
    '%(asctime)s %(levelname)s %(process)d %(thread)d '
    '%(pathname)s %(lineno)s %(message)s')


    # FileHandler Info
    file_handler_info = RotatingFileHandler(filename=cls.LOG_PATH_INFO)
    file_handler_info.setFormatter(formatter)
    file_handler_info.setLevel(logging.INFO)
    info_filter = InfoFilter()
    file_handler_info.addFilter(info_filter)
    app.logger.addHandler(file_handler_info)

    # FileHandler Error
    file_handler_error = RotatingFileHandler(filename=cls.LOG_PATH_ERROR)
    file_handler_error.setFormatter(formatter)
    file_handler_error.setLevel(logging.ERROR)
    app.logger.addHandler(file_handler_error)


    class TestingConfig(Config):
    TESTING = True
    SQLALCHEMY_DATABASE_URI = os.environ.get('TEST_DATABASE_URL') or
    'sqlite:///' + os.path.join(basedir, 'data-test.sqlite')
    WTF_CSRF_ENABLED = False



    config = {
    'development': DevelopmentConfig,
    'testing': TestingConfig,

    'default': DevelopmentConfig
    }
    app.py

    from config import config

    def create_app(config_name):
    app = Flask(__name__)
    app.json_encoder = AlchemyEncoder
    app.config.from_object(config[config_name])
    config[config_name].init_app(app)
    return app
    然后你就可以在其他任何一个模块记录log

    from flask import current_app

    try:
    s = int('ss')
    except ValueError as e:
    current_app.logger.exception(e)
    error.log

    2018-07-02 21:44:12,976 ERROR 4848 139663008261888 /home/.../xxx.py 33 invalid literal for int() with base 10: 'ss'
    Traceback (most recent call last):
    File "/home/.../xxx.py", line 31, in show_product_manage
    s = int('ss')
    ValueError: invalid literal for int() with base 10: 'ss'
    ---------------------
    作者:zwxiaoliu
    来源:CSDN
    原文:https://blog.csdn.net/zwxiaoliu/article/details/80890136
    版权声明:本文为博主原创文章,转载请附上博文链接!

  • 相关阅读:
    【算法笔记】B1020 月饼
    JZOJ 3412. 【NOIP2013模拟】KC看星
    JZOJ 3517. 空间航行
    JZOJ 3515. 软件公司
    JZOJ 3514. 最小比例
    JZOJ 3490. 旅游(travel)
    luogu P3178 [HAOI2015]树上操作
    JZOJ 3427. 归途与征程
    JZOJ 3426. 封印一击
    JZOJ 3425. 能量获取
  • 原文地址:https://www.cnblogs.com/ExMan/p/10162657.html
Copyright © 2011-2022 走看看