一:项目架构

二:自定义日志类

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

flask中自定义日志类的更多相关文章

  1. Python3自定义日志类教程

    一.说明 Python3的logging功能是比较丰富的支持不同层次的日志输出,但或是我们想在日志前输出时间.或是我们想要将日志输入到文件,我们还是想要自定义日志类. 之前自己也尝试写过但感觉文档太乱 ...

  2. Flask之自定义模型类

    4.3自定义模型类 定义模型 模型表示程序使用的数据实体,在Flask-SQLAlchemy中,模型一般是Python类,继承自db.Model,db是SQLAlchemy类的实例,代表程序使用的数据 ...

  3. 关于MapReduce中自定义分区类(四)

    MapTask类 在MapTask类中找到run函数 if(useNewApi){       runNewMapper(job, splitMetaInfo, umbilical, reporter ...

  4. 关于MapReduce中自定义分组类(三)

    Job类  /**    * Define the comparator that controls which keys are grouped together    * for a single ...

  5. python3.4中自定义数组类(即重写数组类)

    '''自定义数组类,实现数组中数字之间的四则运算,内积运算,大小比较,数组元素访问修改及成员测试等功能''' class MyArray: '''保证输入值为数字元素(整型,浮点型,复数)''' de ...

  6. shell脚本中自定义日志记录到文件

    自定义日志函数和前期变量 # adirname - return absolute dirname of given file adirname() { odir=`pwd`; cd `dirname ...

  7. 如何自定义Flask中的响应类

    http://codingpy.com/article/customizing-the-flask-response-class/

  8. Python3自定义日志类 mylog

    #encoding=utf-8 import os, sysimport datetimeimport time class Mylog(object): # 根文件夹    root_dir = s ...

  9. 关于MapReduce中自定义Combine类(一)

    MRJobConfig      public static fina COMBINE_CLASS_ATTR      属性COMBINE_CLASS_ATTR = "mapreduce.j ...

随机推荐

  1. mount -o是什么意思

    mount -o是用loop设备, 在 linux挂载本地的(可能是硬盘上的) iso文件时, 使用的. -o 就是loop回环设备的意思. loop回路文件系统: 是: 用来在一个文件系统上实现另一 ...

  2. python是强类型还是弱类型语言

    几句话了解python特性 Python 是强类型的动态脚本语言 好多人对python到底是强语言类型还是弱语言类型存在误解,其实,是否是强类型语言只需要一句话就可以判别, 强类型:不允许不同类型相加 ...

  3. Go开发[八]goroutine和channel

    进程和线程 进程是程序在操作系统中的一次执行过程,系统进行资源分配和调度的一个独立单位. 线程是进程的一个执行实体,是CPU调度和分派的基本单位,它是比进程更小的能独立运行的基本单位. 一个进程可以创 ...

  4. 是否被封禁ip或端口的检测网站 ping

    国内的: http://tool.chinaz.com/port (可以检测端口) https://tools.ipip.net/ping.php (貌似不可以检测端口) 国外的: https://w ...

  5. 【命令汇总】Windows 应急响应

    日期:2019-06-07 16:11:49 作者:Bay0net 介绍:Windows 应急响应.取证及溯源相关内容学习记录 0x00.前言 常见的应急分类: web入侵:网页挂马.主页篡改.Web ...

  6. TCP中SYN洪水攻击

    在查看TCP标识位SYN时,顺便关注了一下SYN Flood,从网上查阅一些资料加以整理,SYN洪水攻击利用TCP三次握手. 1.SYN洪水介绍 当一个系统(客户端C)尝试和一个提供了服务的系统(服务 ...

  7. maven spark Scala idea搭建maven项目的 pom.xml文件配置

    1.pom.xml文件配置,直接上代码. <?xml version="1.0" encoding="UTF-8"?> <project xm ...

  8. python+selenium下弹窗alter对象处理01

    alt.accept() :                            等同于单击“确认”或者“OK” alt.dismiss() :                            ...

  9. java 接入微信 spring boot 接入微信

    1.pom.xml <?xml version="1.0" encoding="UTF-8"?> <project xmlns="h ...

  10. [Web 前端] 027 jQuery 相关尺寸与事件绑定

    1. 相关尺寸 1.1 获取元素相对于文档的偏移量 var pos = $('#small').offset(); console.log(pos.left, pos.top); 1.2 获取当前元素 ...