1. def add_error_handler(app):
  2. for exception in default_exceptions:
  3. app.register_error_handler(exception, error_handler)
  4. app.register_error_handler(Exception, error_handler)
  5.  
  6. def error_handler(error):
  7. description = error.get_description(request.environ) \
  8. if hasattr(error, 'get_description') else None
  9.  
  10. exc_type, exc_value, exc_traceback = sys.exc_info()
  11. traceback.print_exception(exc_type, exc_value, exc_traceback)
  12.  
  13. current_app.logger.error(
  14. """
  15. Request: {method} {path}
  16. IP: {ip}
  17. User: {user}
  18. Agent: {agent_platform} | {agent_browser} {agent_browser_version}
  19. Raw Agent: {agent}
  20. Description: {description}
  21. """.format(
  22. method=request.method,
  23. path=request.path,
  24. ip=request.remote_addr,
  25. agent_platform=request.user_agent.platform,
  26. agent_browser=request.user_agent.browser,
  27. agent_browser_version=request.user_agent.version,
  28. agent=request.user_agent.string,
  29. user=g.user if hasattr(g, 'user') else None,
  30. description=description,
  31. ), exc_info=exc_traceback
  32. )
  33.  
  34. if isinstance(error, HTTPException):
  35. code = error.code
  36. if code == 404:
  37. return make_response(jsonify({'code': 404, 'data': {}, 'msg': 'page not found'}), code)
  38. else:
  39. """其他HTTP异常处理暂未定义"""
  40. pass
  41. else:
  42. current_app.logger.error('Internal Server Error')
  43. return make_response(jsonify({'code': 500, 'data': {}, 'msg': 'Internal Server Error'}))
  1. app = Flask(__name__)
  2. add_error_handler(app)

  

flask 添加日志的更多相关文章

  1. Flask--(项目准备)--添加日志

    日志:记录程序运行的状态,在manage.py同级目录下创建logs文件夹 定义日志文件: import logging from logging.handlers import RotatingFi ...

  2. logging 为全局的日志工具对象添加日志记录器

    def log_file(LEVEL_NAME): # 设置日志的记录等级,常见的有四种,大小关系如下,DEBUG < INFO < WARNING <ERROR # 一旦设置级别, ...

  3. 测试平台系列(3) 给Hello World添加日志

    给Hello World添加日志 回顾 通过上篇内容,我们已经使用「Flask」完成了我们的第一个接口.我们可以看到,使用「Flask」来编写接口是十分简单的.那么接下来,我们丰富一下上面的例子. 需 ...

  4. ASP.NET Core 添加日志NLog

    1.在Nuget上搜索 NLog.Extensions.Logging 安装最新版 2.添加日志配置文件,在项目指定目录下添加配置文件nlog.config,内容添加如下: <?xml vers ...

  5. (译)Windsor入门教程---第五部分 添加日志功能

    介绍     现在我们已经有了基础的框架了,是时候添加内容了,那么我们首先应该考虑的就是在应用程序中添加日志功能.我们会使用Windsor来配置,在这一部分,你将学习Windsor之外的其他功能. L ...

  6. python web开发-flask中日志的使用

    Flask使用日志记录的方式: 初始化flask应用实例 在flask中使用logger,需要初始化一个flask的应用 app = Flask(__name__) 2. 调用logger 直接调用l ...

  7. net core中动态给log4net添加日志类型

    private static object m_Lock = new object(); /// <summary> /// 根据类型获取对应的日志操作类 /// </summary ...

  8. ThinkPHP执行调用存储过程添加日志

    本文出至:新太潮流网络博客 //PHP代码部分 /** * [LogAdd 操作日志] * @param [string] $userid [用户的ID] * @param [string] $typ ...

  9. SSIS教程:创建简单的ETL包 -- 3. 添加日志(Adding Logging)

    Microsoft Integration Services 包含日志记录功能,可通过提供任务和容器事件跟踪监控包执行情况以及进行故障排除. 日志记录功能非常灵活,可以在包级别或在包中的各个任务和容器 ...

随机推荐

  1. 学习di'z地址

    Swift学习地址https://numbbbbb.gitbooks.io/-the-swift-programming-language-/content/ http://www.oschina.n ...

  2. nodejs gulp less编辑

    nodejs 下载地址:http://pan.baidu.com/s/1bnz2oPp 提取密码:0mip 1.最简单的编译less 在安装好nodejs后,cmd 进入控制台 安装 lessc : ...

  3. ROS 使用自带和usb摄像头获取图像

    笔记本自带的摄像头的设备号一般为/dev/video0 第一步:安装Webcam 驱动 $ sudo apt-get install git-core $ cd ~/catkin_ws/src $ g ...

  4. Hibernate 检索方式

    概述 •Hibernate 提供了以下几种检索对象的方式 –导航对象图检索方式:  根据已经加载的对象导航到其他对象 –OID 检索方式:  按照对象的 OID 来检索对象 –HQL 检索方式: 使用 ...

  5. [转]Android,Yocto,Meego构建系统的区别

    http://m.blog.csdn.net/blog/sonach_tjsd/6647829

  6. jetty启动不能保存

    主要原因是jetty缓存的静态页面不能被修改.只需要在web.xml文件中配置如下: <servlet>    <!-- Override init parameter to avo ...

  7. mysql之存储过程

    一.存储过程     迄今为止,使用的大多数 SQL语句都是针对一个或多个表的单条语句.并非所有操作都这么简单,经常会有一个完整的操作需要多条语句才能完成.例如,考虑以下的情形.         1. ...

  8. realestate.cei.gov.cn

    using AnfleCrawler.Common; using System; using System.Collections.Concurrent; using System.Collectio ...

  9. Wireshark找不到网络接口问题

    Wireshark找不到网络接口问题 在运行Wireshark工具抓包时,需要有管理员用户权限.如果是普通用户启动的话,将会提示找不到网络接口.

  10. UE4 编译后 不能正常使用Open Level 打开关卡解决方案:Open Level Blueprint Node not workin

    配置DefaultEditor.ini 文件 [AllMaps] +Map=/关卡文件路径   参考文献: https://answers.unrealengine.com/questions/141 ...