gpt4 book ai didi

python Elasticsearch : how do you enable curl logging?

转载 作者:行者123 更新时间:2023-12-02 22:45:53 25 4
gpt4 key购买 nike

我正在使用 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 日志记录?

我试过:

  • 将全局记录器设置为 logging.basicConfig(level=logging.DEBUG) 但没有输出 curl
  • 我尝试获取 elasticsearch.trace 记录器并将该记录器的级别设置为 logging.DEBUG 然后设置 es_trace_logger.propagate = True 但是这些都没有用

最佳答案

我认为您可能遗漏的一个关键步骤是向 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 logger会输出这样的内容——

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","_

关于 python Elasticsearch : how do you enable curl logging?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52783266/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com