Opster_support
(Elasticsearch community support @ Opster)
2
Hi, yes, the Elasticsearch Python client supports logging of requests made to the Elasticsearch backend. You can enable logging by configuring the Python logging module. Here's an example of how to do it:
import logging
from elasticsearch import Elasticsearch
# Enable Elasticsearch logging
es_logger = logging.getLogger('elasticsearch')
es_logger.setLevel(logging.INFO)
# Create console handler
console_handler = logging.StreamHandler()
console_handler.setLevel(logging.INFO)
# Create formatter
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
# Add formatter to console handler
console_handler.setFormatter(formatter)
# Add console handler to logger
es_logger.addHandler(console_handler)
# Now, when you make a request, it will be logged
es = Elasticsearch(hosts="http://test-elastic-host:9200")
response = es.search(index="my-index-000001", body={"query": {"match_all": {}}})
In this example, the logger is set to log at the INFO level, which will log all the requests made to the Elasticsearch backend. You can adjust the logging level as per your needs. The logs will be printed to the console because of the StreamHandler. If you want to log to a file, you can use a FileHandler instead.
Remember to close the logger and the Elasticsearch connection when you're done:
# Close the connection and logger when done
es.transport.close()
for handler in es_logger.handlers:
handler.close()
es_logger.removeHandler(handler)
This will give you a detailed log of all the requests made to the Elasticsearch backend, similar to Django's SQL statement logging.
Side note: opsgpt.io helped me with part of this answer.
Apache, Apache Lucene, Apache Hadoop, Hadoop, HDFS and the yellow elephant
logo are trademarks of the
Apache Software Foundation
in the United States and/or other countries.