zoukankan      html  css  js  c++  java
  • flask中自定义日志类

    一:项目架构

    二:自定义日志类

    1. 建立log.conf的配置文件

    log.conf

    [log]
    LOG_PATH = /log/
    LOG_NAME = info.log

     2. 定义日志类

    LogClass.py

    import logging
    from logging import handlers
    
    class Mylogger(object):
        def __init__(self,log_path,log_name):
            # 1.指明日志记录到哪个文件 "F:/xxx/xx" + "info.log"
            logfile = log_path + log_name
            # 2.配置日志操作器
            handler = handlers.RotatingFileHandler(logfile, maxBytes=1024 * 1024, backupCount=5, encoding='utf-8')
            # 3.设置日志格式
            fmt = "%(levelname)s-%(asctime)s-%(module)s-%(lineno)d-%(message)s"
            # 4. 配置格式实例
            formatter = logging.Formatter(fmt)
            # 5.操作器加载格式实例
            handler.setFormatter(formatter)
            # 6.创建logger实例
            self.logger = logging.getLogger()
            # 7.给实例增加日志操作器
            self.logger.addHandler(handler)
            # 8.给实例增加日志输出登记
            self.logger.setLevel(logging.DEBUG)
      # 设置方法返回looger实例
    def get_logger(self): return self.logger

     三:视图中使用logger日志

    user_api.py

    from flask import Flask,request,jsonify
    from flask_cors import CORS
    from log.LogClass import Mylogger
    import os
    import configparser
    app = Flask(__name__)
    CORS(app,supports_credentials=True)
    # 1.获取根目录
    root_path = os.path.split(os.path.realpath(__file__))[0]
    # 2. 设置日志解析实例
    cf = configparser.ConfigParser()
    # 3.读取日志文件
    cf.read(root_path+"/config/log.conf")
    # 4. 创建自定义日志类的实例对象
    logger = Mylogger(root_path + cf.get("log","LOG_PATH"),cf.get("log","LOG_NAME")).get_logger()
    
    
    @app.route("/index",methods=["POST","GET"])
    def demo():
        try:
            print(1/0)
        except Exception as e:
            logger.error(e)
    
    if __name__ == '__main__':
        app.run(debug=True)

     运行程序后 访问 127.0.0.1:5000/index,在log文件夹里面增加了info.log文件

     查看info.log

    INFO-2019-12-10 14:36:22,124-_internal-122- * Restarting with stat
    WARNING-2019-12-10 14:36:22,590-_internal-122- * Debugger is active!
    INFO-2019-12-10 14:36:22,594-_internal-122- * Debugger PIN: 259-203-506
    INFO-2019-12-10 14:36:22,602-_internal-122- * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
    ERROR-2019-12-10 14:36:25,475-user_api-23-division by zero
    INFO-2019-12-10 14:36:25,480-_internal-122-127.0.0.1 - - [10/Dec/2019 14:36:25] "GET /index HTTP/1.1" 500 -
    INFO-2019-12-10 14:36:25,497-_internal-122-127.0.0.1 - - [10/Dec/2019 14:36:25] "GET /index?__debugger__=yes&cmd=resource&f=style.css HTTP/1.1" 200 -
    INFO-2019-12-10 14:36:25,498-_internal-122-127.0.0.1 - - [10/Dec/2019 14:36:25] "GET /index?__debugger__=yes&cmd=resource&f=debugger.js HTTP/1.1" 200 -
    INFO-2019-12-10 14:36:25,498-_internal-122-127.0.0.1 - - [10/Dec/2019 14:36:25] "GET /index?__debugger__=yes&cmd=resource&f=jquery.js HTTP/1.1" 200 -
    INFO-2019-12-10 14:36:25,537-_internal-122-127.0.0.1 - - [10/Dec/2019 14:36:25] "GET /index?__debugger__=yes&cmd=resource&f=ubuntu.ttf HTTP/1.1" 200 -
    INFO-2019-12-10 14:36:25,581-_internal-122-127.0.0.1 - - [10/Dec/2019 14:36:25] "GET /index?__debugger__=yes&cmd=resource&f=console.png HTTP/1.1" 200 -
    INFO-2019-12-10 14:36:25,626-_internal-122-127.0.0.1 - - [10/Dec/2019 14:36:25] "GET /index?__debugger__=yes&cmd=resource&f=console.png HTTP/1.1" 200 -

     将LogClass.py中 self.logger.setLevel(logging.DEBUG) 改为 self.logger.setLevel(logging.ERROR),然后运行程序,查看info.log

    INFO-2019-12-10 14:40:12,643-_internal-122- * Detected change in 'F:\info\log\LogClass.py', reloading
    INFO-2019-12-10 14:40:12,673-_internal-122- * Restarting with stat
    WARNING-2019-12-10 14:40:13,135-_internal-122- * Debugger is active!
    INFO-2019-12-10 14:40:13,139-_internal-122- * Debugger PIN: 259-203-506
    INFO-2019-12-10 14:40:13,147-_internal-122- * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
    ERROR-2019-12-10 14:40:17,367-user_api-23-division by zero
    INFO-2019-12-10 14:40:17,372-_internal-122-127.0.0.1 - - [10/Dec/2019 14:40:17] "GET /index HTTP/1.1" 500 -
    INFO-2019-12-10 14:40:17,388-_internal-122-127.0.0.1 - - [10/Dec/2019 14:40:17] "GET /index?__debugger__=yes&cmd=resource&f=style.css HTTP/1.1" 200 -
    INFO-2019-12-10 14:40:17,389-_internal-122-127.0.0.1 - - [10/Dec/2019 14:40:17] "GET /index?__debugger__=yes&cmd=resource&f=jquery.js HTTP/1.1" 200 -
    INFO-2019-12-10 14:40:17,389-_internal-122-127.0.0.1 - - [10/Dec/2019 14:40:17] "GET /index?__debugger__=yes&cmd=resource&f=debugger.js HTTP/1.1" 200 -
    INFO-2019-12-10 14:40:17,427-_internal-122-127.0.0.1 - - [10/Dec/2019 14:40:17] "GET /index?__debugger__=yes&cmd=resource&f=ubuntu.ttf HTTP/1.1" 200 -
    INFO-2019-12-10 14:40:17,466-_internal-122-127.0.0.1 - - [10/Dec/2019 14:40:17] "GET /index?__debugger__=yes&cmd=resource&f=console.png HTTP/1.1" 200 -
    INFO-2019-12-10 14:40:17,511-_internal-122-127.0.0.1 - - [10/Dec/2019 14:40:17] "GET /index?__debugger__=yes&cmd=resource&f=console.png HTTP/1.1" 200 -

     # 具体原因 TODO

  • 相关阅读:
    各位AS3各种验证在这里,邮箱 身份证 ...
    各位同学还在为AS3在IE透明模式下弹出新窗口而烦恼吗?
    Flash As3 通过二进制[ByteArray]判断真实的文件类型
    【A8笔记1】Alternativa 8.5.0 在Flash、Fb、Fd中的配置
    超酷光带效果
    flash 墙
    A3D CoverFlow图片展示效果
    Windows8Metro模式IE10放弃Flash的支持
    html5 控件整理
    AS3中JSON的基本应用实例
  • 原文地址:https://www.cnblogs.com/meloncodezhang/p/12016594.html
Copyright © 2011-2022 走看看