python-elasticsearch:如何启用 curl 日志记录?

python-elasticsearch: how do you enable curl logging?

我正在使用 python-elasticsearch 模块,我在 python-elasticsearch documentation that you can log all the underlying HTTP requests as command line curl commands:

中阅读

elasticsearch.trace can be used to log requests to the server in the form of curl commands using pretty-printed json that can then be executed from command line. Because it is designed to be shared (for example to demonstrate an issue) it also just uses localhost:9200 as the address instead of the actual address of the host. If the trace logger has not been configured already it is set to propagate=False so it needs to be activated separately.

对于 python-elasticsearch 模块,如何启用此 curl 日志记录?

我试过了:

我认为您可能遗漏的一个关键步骤是向 elasticsearch.trace 记录器添加一个处理程序。

import logging
es_trace_logger = logging.getLogger('elasticsearch.trace')
es_trace_logger.setLevel(logging.DEBUG)
handler = logging.StreamHandler()
es_trace_logger.addHandler(handler)

所以这里我在记录器中添加了一个StreamHandler,所以所有的日志都会转到stdout。如果需要,您可以根据您的用例添加不同的处理程序,例如 FileHandler

这是相同的示例调试日志 -

curl -XGET 'http://localhost:9200/my_index/_search?pretty' -d '{
  "size": 100
}'
#[200] (1.311s)
#{
#  "_shards": {
#    "failed": 0,
#    "successful": 6,
#    "total": 6
#  },
#  "hits": {
#    "hits": [
#      {
#        "_id": "FLKSD0SDFJJSDF7D518319DE5EEBB5d5b07044",

拥有此记录器将记录我们发出的每个请求的整个请求和响应,因此有时这些日志可能会让人不知所措,但非常适合调试。

对于同一个请求,对应的elasticsearch记录器会输出这样的东西-

GET http://my_es_host:9200/my_index/_search [status:200 request:1.528s]
> {"size": 100}
< {"took":21,"timed_out":false,"_shards":{"total":6,"successful":6,"failed":0},"hits":{"total":112,"max_score":1.0,"hits":[{"_index":"my_index","_