当前位置:网站首页>How do I write Flask's excellent debug log message to a file in production?
How do I write Flask's excellent debug log message to a file in production?
2022-07-06 22:22:00 【Bustling city 】
problem :
I have a Flask application that works well and produces an occasional error, which is visible when it is running with debug=True
: I have one Flask The application works well , Occasionally, an error occurs , When it is used debug=True
Visible at runtime :
if __name__ == '__main__': app.run(debug=True)
I get useful error messages such as: I received useful error messages , for example :
Traceback (most recent call last): File "./main.py", line 871, in index_routeKeyError: 'stateIIIII'
I would like to get error messages like these saved to a file when I run the application in production (using Lighttpd + fastcgi). When I run an application in a production environment ( Use Lighttpd + fastcgi) when , I want to save these error messages to a file .
After looking at various StackOverflow questions ( http://flask.pocoo.org/docs/errorhandling/ , http://docs.python.org/2/library/logging.html , etc.); After checking various StackOverflow problem ( http://flask.pocoo.org/docs/errorhandling/ 、 http://docs.python.org/2/library/logging.html etc. ) after ;the Flask mailing list;Flask Mailing list, ;and a few blogs, it seems there is no easy way just to send all the great error messages to a file - I need to use the Python logging module to customise things. And some blogs , There seems to be no simple way to send all important error messages to the file - I need to use Python The logging module defines the content .So I came up with the following code. So I came up with the following code .
At the top of my application file I have various imports followed by: At the top of my application file , I have all kinds of import , And then there was :
app = Flask(__name__)if app.debug is not True: import logging from logging.handlers import RotatingFileHandler file_handler = RotatingFileHandler('python.log', maxBytes=1024 * 1024 * 100, backupCount=20) file_handler.setLevel(logging.ERROR) app.logger.setLevel(logging.ERROR) app.logger.addHandler(file_handler)
I have then put the code for each route in a try/except statement and use traceback to work out which line the error came from and print a nice error message: Then I put the code of each route in try/except In the sentence , And use backtracking to determine which line the error came from and print a good error message :
def some_route(): try: # code for route in here (including a return statement) except: exc_type, exc_value, exc_traceback = sys.exc_info() app.logger.error(traceback.print_exception(exc_type, exc_value, exc_traceback, limit=2)) return render_template('error.html')
And then right at the end of the file I remove the debug=True
statement. Then at the end of the file , I deleted debug=True
sentence .Though I don't think I need to do that as the application is being run by a fastcgi server(?) when it is run in production. Although I don't think I need to do this , Because the application is run in production by fastcgi The server (?) function .The last two lines of my application code look like this: The last two lines of my application code are shown below :
if __name__ == '__main__': app.run()
I am struggling to get this working. I'm trying to make this work .I think the best I have managed is to get a single error log message to be saved in the file using ( app.logger.error('test message')
), but it only prints that one message. I think the best way I manage is to use ( app.logger.error('test message')
) Save a single error log message in a file , But it only prints a message .An attempt to log another error directly after that one is simply ignored. An attempt to record another error directly after this error is simply ignored .
Solution :
Reference resources : https://stackoom.com/en/question/wtux边栏推荐
- 【数字IC手撕代码】Verilog无毛刺时钟切换电路|题目|原理|设计|仿真
- 剪映+json解析将视频中的声音转换成文本
- VIP case introduction and in-depth analysis of brokerage XX system node exceptions
- 2022-07-05 使用tpcc对stonedb进行子查询测试
- GPS从入门到放弃(十一)、差分GPS
- HDU 2008 digital statistics
- What a new company needs to practice and pay attention to
- Inno Setup 打包及签名指南
- GPS from entry to abandonment (XVII), tropospheric delay
- Oracle-控制文件及日志文件的管理
猜你喜欢
嵌入式常用计算神器EXCEL,欢迎各位推荐技巧,以保持文档持续更新,为其他人提供便利
Unity3d minigame-unity-webgl-transform插件转换微信小游戏报错To use dlopen, you need to use Emscripten‘s...问题
GPS du début à l'abandon (XIII), surveillance autonome de l'intégrité du récepteur (raim)
Powerful domestic API management tool
Memorabilia of domestic database in June 2022 - ink Sky Wheel
About the professional ethics of programmers, let's talk about it from the way of craftsmanship and neatness
第4章:再谈类的加载器
手写ABA遇到的坑
【sciter】: 基于 sciter 封装通知栏组件
C#实现水晶报表绑定数据并实现打印4-条形码
随机推荐
Hardware development notes (10): basic process of hardware development, making a USB to RS232 module (9): create ch340g/max232 package library sop-16 and associate principle primitive devices
Barcodex (ActiveX print control) v5.3.0.80 free version
GPS from getting started to giving up (XI), differential GPS
0 basic learning C language - digital tube
墨西哥一架飞往美国的客机起飞后遭雷击 随后安全返航
LeetCode 练习——剑指 Offer 26. 树的子结构
GPS从入门到放弃(十二)、 多普勒定速
(十八)LCD1602实验
2022-07-05 stonedb的子查询处理解析耗时分析
Force buckle 575 Divide candy
Anaconda installs third-party packages
2022-07-05 使用tpcc对stonedb进行子查询测试
labelimg的安装与使用
CCNA Cisco network EIGRP protocol
GPS from getting started to giving up (12), Doppler constant speed
GPS從入門到放弃(十三)、接收機自主完好性監測(RAIM)
[MySQL] online DDL details
Problems in the process of opencv300 cmake generating project
VIP case introduction and in-depth analysis of brokerage XX system node exceptions
Management background --4, delete classification