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

 

 

 

 

 

 

 

 

 

 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM