一:项目架构

二:自定义日志类

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. ES6字符串的拓展

    字符串的遍历接口 for...of循环遍历. for (let codePoint of 'foo') { console.log(codePoint) } // "f" // & ...

  2. centos官网镜像下载方法

    1.CentoS简介 CentOS(Community Enterprise Operating System,社区企业操作系统)是一个基于Red Hat Linux 提供的可自由使用源代码的企业级L ...

  3. Linux几个命令的升级替代品

    grep => ack, agack和ag是两个文本搜索工具,比自带的grep要好用得多.在指定目录下搜索文本时,它们不需要像grep那样指定各种命令行选项,输出结果也会包含文件名和行号,并且会 ...

  4. 【Linux 应用编程】文件IO操作 - 常用函数

    Linux 系统中的各种输入输出,设计为"一切皆文件".各种各样的IO统一用文件形式访问. 文件类型及基本操作 Linux 系统的大部分系统资源都以文件形式提供给用户读写.这些文件 ...

  5. es笔记---新建es索引

    es对索引的一堆操作都是用restful api去进行的,参数时一堆json,一年前边查边写搞过一次,这回搞迁移,发现es都到6.0版本了,也变化了很多,写个小笔记记录一下. 创建一个es索引很简单, ...

  6. uwsgi + nginx 部署python项目(二)

    实现负载均衡 开启两个服务器,nginx负责分发请求到两个服务器,以减轻单个服务器负担. 配置uwsgi服务器 在a项目目录下生成uwsgi.ini文件,在b项目目录下生成uwsgi.ini文件,如何 ...

  7. CentOS7编译安装sshpass过程

    环境说明:centos 7 cat /etc/redhat-release CentOS Linux release 7.6.1810 (Core) 我的sshpass版本 sshpass-1.06. ...

  8. Ubuntu 安装nodejs最新版本

    sudo apt update -y   sudo apt install -y npm   sudo npm config set registry https://registry.npm.tao ...

  9. LINUX “软链接”和“硬链接”的区别

    今天在知乎上看到一篇十分有趣的问题: 如何评价微软高级工程师痴迷于soft link这一linux常见概念? 虽然又是知名撕逼王曾某的撕逼帖,但是我还是想就题目中链接的问题简单地讲讲. 什么是链接? ...

  10. How to attach multiple files in the Send Mail Task in SSIS

    Let’s say you need to create a SSIS package that creates 2 files and emails the files to someone. Yo ...