如何通过python日志从.ini文件配置运行HttpHandler?

如何通过python日志从.ini文件配置运行HttpHandler?,python,logging,configuration,ini,httphandler,Python,Logging,Configuration,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: %(me

我已经通过.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日志记录的实际解决方法。

您的应用程序文件中缺少一个
返回值以及获取日志的方法。您可以尝试(另请参见):

$cat app.py
从烧瓶进口烧瓶,请求
导入json
app=烧瓶(名称)
@app.route('/httplogtest',methods=['POST','GET'])
def handle_http_log():
打印(f'错误日志应该在这里:{request.form.get(“msg”)}')
打印(json.dumps(request.form))
返回“”
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
运行(host='localhost',port=8080,debug=True)
$python3 app.py
*服务烧瓶应用程序“应用程序”(延迟加载)
*环境:生产
警告:这是一个开发服务器。不要在生产部署中使用它。
改用生产WSGI服务器。
*调试模式:打开
*继续http://localhost:8080/ (按CTRL+C退出)
*使用stat重新启动
*调试器处于活动状态!
*调试器PIN:235-227-512
错误日志应该在这里:这是信息日志
{“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”:“created”:“1619127251.130069”,“msecs”:“130.06901741027832”,“relatived”:“18.280029296875”,“线程”:“4653424064”,“线程名”:“主线程”,“进程名”:“主进程”,“进程”:“82830”}
127.0.0.1---[22/Apr/2021 23:34:11]“POST/httplogtest HTTP/1.1”200-

能否请您详细说明“无法运行Http处理程序”?您会收到一个错误(如果是,错误是什么)、您没有获得日志或其他信息?您还可以通过添加包含
@app.route('/httplogtest',methods=['get',POST']的整个文件来编辑您的问题
?我无法将日志消息写入我的请求。我正在更新路由文件并在此处共享again@NikolaosChatzis您现在可以检查吗?我只是想在我的请求中接收此日志消息,它正在访问url,但我不知道如何传递和接收值。非常感谢。它成功运行。现在我正在接收登录消息我的请求是通过请求。表单。获取(“MSG”)。我想得到在.ini文件中存在的指定格式。它只是通过没有指定格式的简单文本。有什么办法吗?伟大的,请考虑接受和投票,如果答案对您有帮助(参见)。关于格式参见文档中的注释()。。我相信您需要提取您需要的内容并生成符合您需要的字符串。我的分数不足15分,因此无法提高投票率。您能提高我的问题的投票率吗?我可能会得到一些分数,然后可以提高您的答案的投票率。谢谢您的帮助。
$ 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 -