flask中自定义日志类

一:项目架构

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文件

flask中自定义日志类

 查看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

 

 

 

 

 

 

 

 

 

 

 

 

 

上一篇:记录下我用Jenkins打包碰到的坑


下一篇:docker 在push镜像到本地registry出现的500 Internal Server Error