如何解决如何在 python 日志记录中从 .ini 文件配置运行 HttpHandler?
我已经通过 .ini 文件完成了各种日志记录,并且一切正常,但在 HttpHandler 中,我没有将日志消息发送到我的路由中。
这是我的 logging.ini 文件:
[loggers]
keys=root
[logger_root]
level=DEBUG
handlers=httpHandler
[formatters]
keys=simple
[formatter_simple]
format=%(asctime)s [%(levelname)s] %(name)s: %(message)s
[handlers]
keys=httpHandler
[handler_httpHandler]
class=handlers.HTTPHandler
formatter=simple
args=('localhost:8080','/httplogtest','POST')
这是我的 logger.py 文件:
import logging
from logging.config import fileConfig
fileConfig('logging.ini')
logger = logging.getLogger()
# This message should be passed to http call
logger.info("This is INFO log")
我想在我的请求中接收此日志消息。我只想通过 .ini 文件来实现这个功能,而不需要任何自定义处理程序。
from flask import Flask
app = Flask(__name__)
@app.route('/httplogtest',methods=['GET','POST'])
def handle_http_log():
# I need to print this debug message here
print('Error log should be here')
这不是一个重复的问题。我搜索了很多资源,但我没有得到真正的解决方法来仅从 .ini 文件进行 httphandler 日志记录。
解决方法
您的应用程序文件中缺少 return
以及获取日志的方法。你可以试试(另见Get the data received in a Flask request):
$ cat app.py
from flask import Flask,request
import json
app = Flask(__name__)
@app.route('/httplogtest',methods=['POST','GET'])
def handle_http_log():
print(f'Error log should be here: {request.form.get("msg")}')
print(json.dumps(request.form))
return ""
if __name__ == '__main__':
app.run(host='localhost',port = 8080,debug=True)
$ python3 app.py
* Serving Flask app "app" (lazy loading)
* Environment: production
WARNING: This is a development server. Do not use it in a production deployment.
Use a production WSGI server instead.
* Debug mode: on
* Running on http://localhost:8080/ (Press CTRL+C to quit)
* Restarting with stat
* Debugger is active!
* Debugger PIN: 235-227-512
Error log should be here: This is INFO log
{"name": "root","msg": "This is INFO log","args": "()","levelname": "INFO","levelno": "20","pathname": "logger.py","filename": "logger.py","module": "logger","exc_info": "None","exc_text": "None","stack_info": "None","lineno": "9","funcName": "<module>","created": "1619127251.130069","msecs": "130.06901741027832","relativeCreated": "18.280029296875","thread": "4653424064","threadName": "MainThread","processName": "MainProcess","process": "82830"}
127.0.0.1 - - [22/Apr/2021 23:34:11] "POST /httplogtest HTTP/1.1" 200 -
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。