使用gunicorn作为HTTP Server的时候,必须手动指派gunicorn的logger作为flask的logger handler,才能够正常的进行DEBUG、WARN、INFO、WARN、CRIT等的log纪录
在Server.py内加入如下程序码,以将gunicorn的logger与flask App结合
if __name__ != '__main__':
gunicorn_logger = logging.getLogger('gunicorn.error')
app.logger.handlers = gunicorn_logger.handlers
app.logger.setLevel(gunicorn_logger.level)
指定logging以debug模式运作,修改Procfile内指令为:
web: gunicorn --log-level=debug Server:app
logger的使用方式:
# app.logger.[LEVEL]([LOG_MESSAGE])
app.logger.debug('this is a DEBUG message')
app.logger.info('this is an INFO message')
app.logger.warning('this is a WARNING message')
app.logger.error('this is an ERROR message')
app.logger.critical('this is a CRITICAL message')
在Server.py内加入一个新的route以测试log效果
@app.route('/')
def default_route():
"""Default route"""
app.logger.debug('this is a DEBUG message')
app.logger.info('this is an INFO message')
app.logger.warning('this is a WARNING message')
app.logger.error('this is an ERROR message')
app.logger.critical('this is a CRITICAL message')
return jsonify('that's a log of logs')
完成所有修改後将修改部署到heroku,测试功能吧
git add .
git commit -m "logging"
git push heroku main
heroku open
herolu logs -t
使用herolu logs 查询logs,你应该可以看到如下输出
2021-09-25T09:19:39.406667+00:00 app[web.1]: [2021-09-25 09:19:39 +0000] [7] [DEBUG] this is a DEBUG message
2021-09-25T09:19:39.406742+00:00 app[web.1]: [2021-09-25 09:19:39 +0000] [7] [INFO] this is an INFO message
2021-09-25T09:19:39.406816+00:00 app[web.1]: [2021-09-25 09:19:39 +0000] [7] [WARNING] this is a WARNING message
2021-09-25T09:19:39.406877+00:00 app[web.1]: [2021-09-25 09:19:39 +0000] [7] [ERROR] this is an ERROR message
2021-09-25T09:19:39.406935+00:00 app[web.1]: [2021-09-25 09:19:39 +0000] [7] [CRITICAL] this is a CRITICAL message
你可以通过Papertrail进行App的状态监测与查看logs,可以省去自行撰写logs分类,与命令列方式查阅不易的问题,并提供各种如错误通知的功能
heroku addons:create papertrail
heroku addons:open papertrail
你可以通过如上addons:open指令开启模组的网页,或是直接以如下方式从浏览器直接进入,将<app name>替换为你的heroku App名称
https://addons-sso.heroku.com/apps/<app name>/addons/papertrail
今天补一点昨天没有忘了补充的部分
>>: 【没钱买ps,PyQt自己写】Day 10 - 以 QFileDialog 读取系统的档案、资料夹
本文介绍 Projucer 建立的 GUI Application 框架基本架构。框架(Framew...
安装的部分都介绍完了,有一个部分大家可能有些困惑,在新增主机 Host 的时候为什麽要加 Templ...
昨天介绍了 Gradle 的 Build Scan 功能,让我们可以清楚的了解 Build 过程中的...
math.h 包含许多数学函数和常数的函式库 常数 M_E 数学中的自然常数 e #include&...
前言 前面几天说了很多CSS\SCSS的介绍和学习方式,终於要踏入JS啦,边学习的过程也可以发现他要...